On the relationship of (LAMBDA (...) ...) and #'(LAMBDA (...) ...).

People frequently ask about the difference between (LAMBDA (...) ...) and #'(LAMBDA (...) ...). Saying there is no difference is, however, an oversimplifcation that leaves out some details from the Common Lisp standard.

In an evaluated context, the relationship between the two forms can be described accurately by saying that LAMBDA is actually a macro that expands the first form into the second one.
(defmacro lambda (&whole whole args &body body)
  (declare (ignore args body))
It follows that in an evaluated context, the two forms are thus completely synonymous. Note that the form involving FUNCTION is strictly more primitive, and is what causes the creation of a lexical closure eventually.

Howsoever, there are some unevaluated contexts in which you're only allowed to use the former, but not the latter form:

  • The CAR of a form must usually be a symbol, naming the function, macro, or special operator that is supposed to be invoked.

    As a special exception to that rule (historically for ISLISP compatibility), the CAR of a form can also be (LAMBDA (...) ...).

    ((lambda (x) (* x x)) 2) ==> 4
    See CLHS section
  • The :interactive, :report, and :test arguments of a clause in RESTART-CASE take (LAMBDA (...) ...), but not #'(LAMBDA (...) ...) forms.
  • Likewise for the :print-function, and :print-object options of DEFSTRUCT.


Robert Synnott said...

Oh, the using a lambda as the car of a form is a new one to me. Yuck.

gigamonkey said...

I think you've got one thing backwards. The thing that was added to support ISLISP compatibilty (as far as I understand it) was the LAMBDA macro. See http://www.lispworks.com/documentation/HyperSpec/Issues/iss198_w.htm for a discussion. And per the online Maclisp manual (c.f. http://www.maclisp.info/pitmanual/def.html#7.1.1) using a lambda expression in the CAR of a form was allowed in Maclisp.

hoa pikey said...

I enjoyed over read your blog post. Your blog have nice information,I got good ideas from this amazing blog. I am always searching like this type blog post. I hope I will see again..
animal jam 2 |2048 game |red ball | stick war 2 |stickman games |five nights at freddy’s 2 |five nights at freddy’s 4 |plants vs zombies |