tags:

views:

108

answers:

1

Hello!

I would like to know why most Common Lisp code I see has things like

(mapcar #'(lambda (x) (* x x)) '(1 2 3))

instead of just

(mapcar (lambda (x) (* x x)) '(1 2 3)),

which seems to work as well. I am beginning to learn Common Lisp, and having some background in Scheme, this intrigues me.

Edit: I know that you need #' with function names because they live in a different namespace than variables. My question is just about #' before lambda, as lambda already returns a function object (I think). The fact that #'-less lambdas work because of a macro expansion just makes it more intriguing...

+6  A: 

#'foo is an abbreviation for (function foo) by the reader.

In CL, there are several different namespaces, #'foo or (function foo) will return the functional value of foo.

You may want to google for "Lisp-1 vs. Lisp-2", check other Stackoverflow questions, or read an old article py Pitman and Gabriel in order to learn more about the concept of multiple namespaces (also called slots or cells of symbols).

The reason that, in the case of lambda the #' may be omitted is, that this is a macro in CL, which expands thusly (taken from the Hyperspec):

(lambda lambda-list [[declaration* | documentation]] form*)
==  (function (lambda lambda-list [[declaration* | documentation]] form*))
==  #'(lambda lambda-list [[declaration* | documentation]] form*)

#' may still be used for historic reasons (I think that in Maclisp lambdas didn't expand to the function form), or because some people think, that "tagging" lambdas with sharpquotes may make the code more readable or coherent. There may be some special cases in which this makes a difference, but in general, it doesn't really matter which form you choose.

I guess you can think of it like this: (function (lambda ...)) returns the function (lambda ...) creates. Note that lambda in the CL Hyperspec has both a macro AND a symbol entry. From the latter:

A lambda expression is a list that can be used in place of a function name in certain contexts to denote a function by directly describing its behavior rather than indirectly by referring to the name of an established function.

From the documentation of function:

If name is a lambda expression, then a lexical closure is returned.

I think the difference is also related to calling lambda forms like this: ((lambda ...) ...) where it is treated as a form to be evaluated, vs. (funcall #'(lambda ...) ...). If you want to read more on the topic, there is a c.l.l thread about it.

Some quotes from that thread:

(lambda (x) ... by itself is just some unquoted list structure. It is its appearance as an argument to the FUNCTION special form (function (lambda (x) ... that causes the function object to exist

and:

It's also compounded by the fact that the LAMBDA macro was a rather late addition the ANSI Common Lisp, so all of the really old guys (i.e., like me) learned their lisp when you needed to supply the #' to the lambda expression in the mapping functions. Otherwise the non-existent lambda function would be invoked.

The macro addition changed that, but some of us are too set in our ways to want to change.

danlei
I am aware of the namespace distinction. But I expected that since lambda returns a function object directly (or does it?), a call to 'function' or #' would not be needed. Why is it so?
Vítor De Araújo
Updated the answer.
danlei
Hmmm, so no, the pure lambda does not return a function object... thanks for the explanation.
Vítor De Araújo
You're welcome.
danlei