Lambda expressions are used to create an object of type function
from other Lisp objects, it is a list whose first element is the
symbol lambda. All functions written in Lisp (as opposed to the
primitive functions in C) are represented by a lambda expression.
Note that a lambda expression is not an expression, evaluating a
lambda expression will give an error (unless there is a function called
lambda).
The format of a lambda expression is:
(lambda lambda-list [doc] [interactive-declaration] body-forms... )
Where lambda-list is the argument specification of the function, doc is an optional documentation string, interactive-declaration is only required by editor commands (see section Commands) and the body-forms is the actual function code (when the function is called each form is evaluated in sequence, the last form's value is the result returned by the function).
The lambda-list is a list, it defines how the argument values applied to the function are bound to local variables which represent the arguments within the function. At its simplest it is simply a list of symbols, each symbol will have the corresponding argument value bound to it. For example, the lambda list,
(lambda (x y) (+ x y))
takes two arguments, x and y. When this function is called
with two arguments the first will be bound to x and the second
to y (then the function will return their sum).
To complicate matters there are several lambda-list keywords which modify the meaning of symbols in the lambda-list. Each keyword is a symbol whose name begins with an ampersand, they are:
&optional
nil.
Note that optional arguments must be specified if a later optional argument
is also specified. Use nil to explicitly show that an optional
argument is undefined.
For example, if a function foo takes two optional arguments and
you want to call it with only the second argument defined, the first
argument must be specified as nil to ensure that the correct argument
value is bound to the correct variable.
(defun foo (&optional arg-1 arg-2) ... (foo nil arg-2-value) ;Leave the first argument undefined
&rest
&rest keyword allows a variable number of arguments to be
applied to a function, all the argument values which have not been
bound to argument variables are simply consed into a list and bound
to the variable after the &rest keyword. For example, in,
(lambda (x &rest y) ...)the first argument,
x, is required. Any other arguments applied
to this function are made into a list and this list is bound to the
y variable.
When a function represented by a lambda-list is called the first thing that happens is to bind the argument values to the argument variables. The lambda-list and the list of argument values applied to the function are worked through in parallel. Any required arguments which are left undefined when the end of the argument values has been reached causes an error.
After the arguments have been processed the body-forms are evaluated by an implicit progn, the value of which becomes the value of the function call. Finally, all argument variables are unbound and control passes back to the caller.
Go to the first, previous, next, last section, table of contents.