www.delorie.com/gnu/docs/guile/guile_254.html   search  
 
Buy GNU books!


Guile Reference Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

23.1 Lambda: Basic Procedure Creation

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 section 14.4 The Concept of 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

syntax: lambda formals body
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.

body is a sequence of Scheme expressions which are evaluated in order when the procedure is invoked.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003