(lispref.info)Simple Lambda

Next: Argument List Prev: Lambda Components Up: Lambda Expressions

A Simple Lambda-Expression Example

   Consider for example the following function:

     (lambda (a b c) (+ a b c))

We can call this function by writing it as the CAR of an expression,
like this:

     ((lambda (a b c) (+ a b c))
      1 2 3)

The body of this lambda expression is evaluated with the variable `a'
bound to 1, `b' bound to 2, and `c' bound to 3.  Evaluation of the body
adds these three numbers, producing the result 6; therefore, this call
to the function returns the value 6.

   Note that the arguments can be the results of other function calls,
as in this example:

     ((lambda (a b c) (+ a b c))
      1 (* 2 3) (- 5 4))

Here all the arguments `1', `(* 2 3)', and `(- 5 4)' are evaluated,
left to right.  Then the lambda expression is applied to the argument
values 1, 6 and 1 to produce the value 8.

   It is not often useful to write a lambda expression as the CAR of a
form in this way.  You can get the same result, of making local
variables and giving them values, using the special form `let' (*note
Local Variables::.).  And `let' is clearer and easier to use.  In
practice, lambda expressions are either stored as the function
definitions of symbols, to produce named functions, or passed as
arguments to other functions (Note: Anonymous Functions.).

   However, calls to explicit lambda expressions were very useful in the
old days of Lisp, before the special form `let' was invented.  At that
time, they were the only way to bind and initialize local variables.

automatically generated by info2www