Question

I am currently reading ANSI Common Lisp by Paul Graham, and I have a question about writing lambda expressions.

Do we need to prefix a lambda expression with #'?. If I write something like this in REPL, it will work fine

> ((lambda (x) (+ x 1)) 1)
  2

so will this

> (mapcar (lambda (x) (+ x x)) '(1 2 3 4))
  (2 4 6 8)

I understand that #' denotes a function. So my question is, is it some sort of convention or recommended practice? Can anything go wrong if I don't prefix lambdas with #', is it implementation dependent?

Was it helpful?

Solution

The LAMBDA expression

(lambda ...) is considered to be a lambda expression only in certain places, like the function form or as the head of a function call. Lambda expressions are not evaluated.

(function              ; special operator FUNCTION
  (lambda () 'foobar)) ; <- this is a lambda expression


(                    ; a function call
 (lambda (foo) foo)  ; <- this is a lambda expression
 'bar                ; argument
)

But here (lambda ...) is a macro form and not a lambda expression:

(funcall             ; calling a function via funcall
 (lambda (foo) foo)  ; this is not a lambda expressions, but the macro lambda
                     ;  as all arguments to FUNCALL it will be
                     ;    macro expanded and evaluated
                     ;  it expands to (function (lambda (foo) foo))
 'bar)               ; argument

The LAMBDA macro

LAMBDA is a macro. It expands (lambda ...) to (function (lambda ...)), which is the equivalent of #'(lambda ...)).

CL-USER > (macroexpand '(lambda (foo) foo))
(FUNCTION (LAMBDA (FOO) FOO))

The macro saves you a bit of writing/reading, that's all. In the first version of Common Lisp (CLtL1) there was no LAMBDA macro. It has been added later and is now a part of ANSI Common Lisp,

The FUNCTION special operator

FUNCTION is a special operator. It expects a function name or a lambda expression. Thus the name or the lambda expression are not evaluated. In fact lambda expressions can't be evaluated at all. Inside FUNCTION, the lambda expression is not a macro form and thus will not be expanded again. The purpose of FUNCTION is to return the corresponding function object which is denoted by the name or by the lambda expression. It returns the function object as a value. With this special operator one can access the function object from global functions and lexical functions.

The FUNCTION operator is necessary in Common Lisp, because it has separate namespaces for values, functions and a few other things. It as a so-called Lisp-2 or even Lisp-n, with two or more namespaces.

Lambda expressions in function position in a function form

((lambda (foo) foo) 10) is supported by built-in syntax for Common Lisp. See Lambda Forms.

Confusing

This is all logical, but confusing. Don't worry you are not alone, but in practice it's not a big deal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top