The notation for an anonymous function uses the special operator called
lambda
and looks like this:
(lambda (
arg-1 ...
arg-m)
exp-1 ...
exp-n)
(lambda (
arg-1 ... arg-m
rest:
arg-m+1)
exp-1 ... exp-n)
(Note: in Common Lisp, this form is prefixed by #'.) It can be used in the function position in a function application:
(lambda (x) ...)
is applied
to the argument 3
. In the second the function
(lambda (x . y) ...)
is applied to the arguments
1
, 2
and 3
, the second
and third of which become the elements of a list bound to y
,
which gives the result.
foo
, which
takes an argument f
and an argument x
and calls the function which is the value of f
with the
arguments x
and (+ x 1)
. Hence, if we call
foo
with +
and 3
we
obtain the result of (+ 3 (+ 1 3))
. In the second case,
f
is a function of two arguments which computes the product
of the two arguments and 2
.
add-n
is a function
of one argument, in which the value of n
is
3
, by virtue of the anonymous functional result being
defined in the environment where n
is associated with
3
. We can visualize this using the same technique as before: