Node:Lambda, Next:Optional Arguments, Up:Procedures and Macros
A lambda
expression evaluates to a procedure. The environment
which is in effect when a lambda
expression is evaluated is
enclosed in the newly created procedure, this is referred to as a
closure (see About Closure).
When a procedure created by lambda
is called with some actual
arguments, the environment enclosed in the procedure is extended by
binding the variables named in the formal argument list to new locations
and storing the actual arguments into these locations. Then the body of
the lambda
expression is evaluation sequentially. The result of
the last expression in the procedure body is then the result of the
procedure invocation.
The following examples will show how procedures can be created using
lambda
, and what you can do with these procedures.
(lambda (x) (+ x x)) => a procedure ((lambda (x) (+ x x)) 4) => 8
The fact that the environment in effect when creating a procedure is
enclosed in the procedure is shown with this example:
(define add4 (let ((x 4)) (lambda (y) (+ x y)))) (add4 6) => 10
lambda formals body | syntax |
formals should be a formal argument list as described in the
following table.
body is a sequence of Scheme expressions which are evaluated in order when the procedure is invoked. |