LAMBDA

LAMBDA form defines and returns an unnamed function object. Apart from that, syntax is the same as DEFUN. First argument must be a list of parameters (symbols) of the function, the symbols are not evaluated. Second argument is optional description string. If the second argument is not string, it is considered first form of the function body. Any other parameters are to LAMBDA are stored as the function body.

>(lambda (a b) (+ a b))   ; lambda call
#<user-defined lambda>    ; function object was returned                

The LAMBDA form can be used in place of the operator.

>((lambda (a b) (+ a b)) 3 5)   ; defined the lambda and immediately used on 3 5
8                

In this example we have created unnamed function object in the first position of the whole form, so it gets applied to the numbers 3 and 5.

The function objects created by the LAMBDA forms can be stored somewhere and called later without the need for a name.

>(setq ops (cons             ; define symbol OPS and its value will be a cons-cell
   (lambda (a b) (+ a b))     ; function for addition is in the CAR
   (lambda (a b) (- a b)) )   ; function for subtraction is in the CDR
(#<user-defined lambda> . #<user-defined lambda>)
 
>((car ops) 4 7)             ; using the adding function
11
 
>((cdr op) 4 7)              ; using the subtraction
-3

Parameter list of LAMBDA can have &OPTIONAL and &REST parameters just like the named functions (see DEFUN).

The functions created by LAMBDA have lexical closure also like the named functions.