3 ms·
Ask HN: Why are lambdas not simply called function literals?
We don't have a special name for any other literals so why do we have one for functions? I get that it's partially for historical reasons but as it has been discussed before this is confusing for anyone new to programming. I have heard them referred to as "anonymous functions" often but I think function literals describes them more accurately.
- db48x 8y agoAlmost all functions are literal, even the ones with names. (A non-literal function in Javascript looks like this: new Function("return 2+2"), which you almost never actually see.) 'Anonymous' describes functions without explicit names, 'closure' describes how functions capture their environment (but again, in most languages all functions capture their environment), and 'lambda' refers back to the "Lambda Calculus" which is a form of mathematics. None of these terms differentiate meaningfully between different types of functions.
- Mosai76 8y agoRight I see your point about most named functions being literals but I think typically when people refer to literals they mean something that can be defined inline or passed into a function without naming it. E.g. map(lambda x: x + 1, [1, 2, 3]) The fact that in popular languages such as C++ you can't even define functions within functions makes referring to the typical way functions are defined as literals quite confusing. I understand why lambdas are called lambdas but I think it is not a very clear or descriptive name.
- informatimago 8y agoActually, there's no difference in named functions and anonymous functions. (define (f x) (* 2 x)) is equivalent to: (define f (lambda (x) (* 2 x))) and like: (define n 42) defines a named integer, there's no fundamental difference between 42 in this define form, and 33 in this list form: (list n 33) In both cases, 42 and 33 are literals. In both cases (lambda (x) (* 2 x)) (the function that has been named f) and (lambda (x y) (+ x (* 3 y))) are function literals. This may seem more puzzling in other programming languages than scheme, but fundamentally this is is it. So yes, the only reason we name them lambda, is historical (and comes from an incomplete understanding of lambda-calculus by John McCarthy at that, since lisp lambda are actually quite different from lambda-calculus lambda, because notably of the differences between the normal-order reduction and the applicative order reduction. Note that some other languages use the keyword function or fn, instead of lambda. In lisp, you can always define a macro to use the keyword you want: (defmacro literal-function ((&rest bindings) &body body) `(lambda ,bindings ,@body)) (funcall (literal-function (x) (* 3 x)) 3) --> 9 But this wouldn't improve anything. Also, note that both named and anonymous functions being function literals, they are immutable (only the binding between the name and the function may be mutable). You cannot modify a function during the execution of the program. Actually, this may be possible with interpreters working from the sexp, but it is totally undefined, and you would only obtain very implementation dependent results. What is generally called "bugs". For example, in emacs lisp you could write: (defun f (x) (let ((a '(1 2 3))) (when (integerp x) (setf (second a) x)) a)) (f nil) --> (1 2 3) (f 0) --> (1 0 3) (f nil) --> (1 0 3) !!! But it is a bug, since you modified the source of the function: '(1 2 3) returns a list literal that should be considered immutable, but it is mutated by the setf form! The bug is that the function should have created a new list each time, with (list 1 2 3) instead of using a literal. So apart the case where you have bugs and undefined behavior, there are very few programming languages where you can mutate a function value. Even in assembler. In older computer it was possible, but nowadays, with cache and pipelines, it's very difficult to mutate the code of a function! So even for run-time computed function objects, there's not much difference between them and literals, since they're both immutable. This is the reason why it is not too meaningful to name function literals, since this doesn't characterize or discriminate anything.
- Mosai76 8y agoNot sure what the immutability of functions has to do with your argument. You may want to edit the Wikipedia page on literals in computer programming where it describes anonymous functions as function literals https://en.wikipedia.org/wiki/Literal_(computer_programming) https://en.wikipedia.org/wiki/Literal_(computer_programming) I know I could redefine the term in my own programs but the problem at large is that it is confusing for newcomers to programming and may deter them from learning about a useful tool. I think it is a bad name and there is surely better alternatives, interested to hear if you have any suggestions.