www.delorie.com/gnu/docs/elisp-manual-21/elisp_163.html   search  
 
Buy the book!


GNU Emacs Lisp Reference Manual

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

12.2.2 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)

This call evaluates the body of the lambda expression 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))

This evaluates the arguments 1, (* 2 3), and (- 5 4) from left to right. Then it applies the lambda expression 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 (see section 11.3 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 (see section 12.7 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.


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

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