lambda
lambda Macro
Syntax:
lambda lambda-list [[ {declaration}* | documentation ]] {form}* → function
Arguments and Values:
lambda-list—an ordinary lambda list.
declaration—a declare expression; not evaluated.
documentation—a string; not evaluated.
form—a form.
function—a function.
Description:
Provides a shorthand notation for a function special form involving a lambda expression such that:
(lambda lambda-list [[ {declaration}* | documentation ]] {form}*)
≡ (function (lambda lambda-list [[ {declaration}* | documentation ]] {form}*))
≡ #’(lambda lambda-list [[ {declaration}* | documentation ]] {form}*)
Examples:
(funcall (lambda (x) (+ x 3)) 4) → 7
See Also:
lambda (symbol)
Notes:
This macro could be implemented by:
(defmacro lambda (&whole form &rest bvl-decls-and-body)
(declare (ignore bvl-decls-and-body))
‘#’,form)
Expanded Reference: lambda (Macro)
Shorthand for #'(lambda ...)
The lambda macro provides convenient shorthand so that (lambda ...) is equivalent to #'(lambda ...) or (function (lambda ...)).
(funcall (lambda (x) (+ x 1)) 10)
=> 11
Passing Lambdas to Higher-Order Functions
Because the macro makes (lambda ...) evaluate to a function, it can be passed directly to higher-order functions.
(mapcar (lambda (x) (* x x)) '(1 2 3 4 5))
=> (1 4 9 16 25)
(remove-if (lambda (x) (evenp x)) '(1 2 3 4 5))
=> (1 3 5)
Equivalence to function Special Form
These three forms are equivalent:
(lambda (x) (+ x 1))
;; is the same as:
#'(lambda (x) (+ x 1))
;; is the same as:
(function (lambda (x) (+ x 1)))
Closures via Lambda
Lambda expressions close over their lexical environment.
(let ((offset 10))
(mapcar (lambda (x) (+ x offset)) '(1 2 3)))
=> (11 12 13)