Warning: This is the manual of the legacy Guile 2.2 series. You may want to read the manual of the current stable series instead.
Next: Primitive Procedures, Up: Procedures [Contents][Index]
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 evaluated 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
formals should be a formal argument list as described in the following table.
(variable1 …)
The procedure takes a fixed number of arguments; when the procedure is called, the arguments will be stored into the newly created location for the formal variables.
variable
The procedure takes any number of arguments; when the procedure is called, the sequence of actual arguments will converted into a list and stored into the newly created location for the formal variable.
(variable1 … variablen . variablen+1)
If a space-delimited period precedes the last variable, then the procedure takes n or more variables where n is the number of formal arguments before the period. There must be at least one argument before the period. The first n actual arguments will be stored into the newly allocated locations for the first n formal arguments and the sequence of the remaining actual arguments is converted into a list and the stored into the location for the last formal argument. If there are exactly n actual arguments, the empty list is stored into the location of the last formal argument.
The list in variable or variablen+1 is always newly
created and the procedure can modify it if desired. This is the case
even when the procedure is invoked via apply
, the required part
of the list argument there will be copied (see Procedures for On the Fly Evaluation).
body is a sequence of Scheme expressions which are evaluated in order when the procedure is invoked.
Next: Primitive Procedures, Up: Procedures [Contents][Index]