Function Lambda

Coming from SchemeLanguage, CommonLisp feels strange in many ways. One is the convention of writing (lambda ...) in some places and #'(lambda ...) or the equivalent (function (lambda ...)) in others. Is there an actual difference between the two, and if there is, what is it?

In SchemeLanguage, normal function application (let's ignore macros and special forms) works like this:

  1. All the positions of the expression are evaluated.
  2. The value in the leftmost position (a procedure) is executed with the rest of the values as parameters.

This works because functions are just variables containing procedures and lambda is a special form creating procedures. An example:

 ((lambda (x) x) (+ 3 5))

(lambda (x) x) evaluates to a procedure which returns whatever we give it, (+ 3 5) evaluates to 8. Then the procedure is executed with 8 as parameter, giving us the result of 8.

In CommonLisp however, there is no special form "lambda", and the first position of an expression is never evaluated. Instead, the first position should be a symbol or a lambda expression, corresponding to a named procedure or creating an anonymous one:

  1. All the positions but the first are evaluated.
  2. The procedure corresponding to the symbol or lambda expression in the first position is executed with the evaluated values as parameters.

In order to be able to pass procedures as parameters, the function special form exists. (function <x>), or #'<x>, means "give <x> the first-position treatment, then return a reference to the resulting procedure". Then, just to mess newcomers up a bit, there exists a macro lambda, which expands to the (function (lambda ...)) form.

So actually the FunctionLambda (or SharpQuote lambda) thing makes sense. You just have to see things a little differently than you're used to.


For further reference:


EditText of this page (last edited December 16, 2005) or FindPage with title or text search