DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH
 

(guile.info.gz) Lambda

Info Catalog (guile.info.gz) Procedures and Macros (guile.info.gz) Optional Arguments
 
 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" (
 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
 
  -- 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.
 
Info Catalog (guile.info.gz) Procedures and Macros (guile.info.gz) Optional Arguments
automatically generated byinfo2html