2008-12-22

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))
  `#',whole)
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 3.1.2.1.2.
  • 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.

5 comments:

rsynnott said...

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

Unknown 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.

hoahero 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 |

Sargun Mehta said...

In case you’re searching out Indian escorts service in Chandigarh, then you definitely have come to the proper vicinity. We're proficient escort business enterprise. We deliver excessive-magnificence escorts to get-collectively, supper dates, lone ranger gatherings, and birthday events.
Follow us:_
Chandigarh escorts
VIP Chandigarh escorts

Chandigarh escorts
Call girls in Chandigarh

Chandigarh escorts service
Escorts service in Chandigarh

Susan said...

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. There are so many fun and exciting things to do and experiences around the world that I thought I ‘d put together a list of favorite Things to do for all travelers.