3 ms·
Actually, there's no difference in named functions and anonymous functions. (define (f x) (* 2 x)) is equivalent to: (define f (lambda (x) (* 2 x)))
by informatimago 8y ago
Actually, 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.