I understand that, because there are separate namespaces in Common Lisp for functions and variables, you can do this:
((lambda (x) (* 2 x)) 3)
and you can also do this:
(funcall #'(lambda (x) (* 2 x)) 3)
When should we use #'
as opposed to not using it? I read in another StackOverflow question that #'
was only kept around for historic reasons and shouldn't be used anymore. Is this true? My question is not a duplicate, I am asking about when I would use these in my code.
It's not an issue of lisp-2 versus lisp-1. With
lambda
expressions in a position where a function value is needed, it's simply a stylistic choice. Some people like the visual marker of#'
and some don't. Thelambda
macro already expands into thefunction
form for which#'
provides an abbreviation:#'x
is just syntactic sugar for(function x)
, and thefunction
special operator provides "the functional value of name in the current lexical environment." TWhile
(lambda ...)
is the name of a function, it's not a name that could ever be established by aflet
,label
, ormacrolet
form, so you're always getting "the global definition of the function name", which is just the lambda function. Since(lambda ...)
expands to(function (lambda ...))
, there's no difference. It's just a matter of style.However, it is important to note that in the first case that you talked about,
you could not do:
The support for
((lambda ...) ...)
is part of the language, unrelated to the fact that there's a definition oflambda
as a macro. It's a particular type of compound form, namely a lambda form, which is described in the HyperSpec: