Is there a difference between `#'(lambda... ` and `(lambda...`?

137 views Asked by At

In Practical Common Lisp there is a example of REMOVE-IF-NOT with a lambda:

CL-USER> (remove-if-not #'(lambda (x) (evenp x)) '(1 2 3 4 5))
(2 4)

Is this any different from:

CL-USER> (remove-if-not (lambda (x) (evenp x)) '(1 2 3 4 5))
(2 4)

Is a (lambda..) value coincident to the quoted-function form #'(..)? On the REPL it seems so, but as I'm new to Lisp I may overlook something (and I surely got the verbiage wrong, so please correct me on that too).

1

There are 1 answers

2
AudioBubble On BEST ANSWER

These two things are, as you suspect, the same:

  • #' is a read macro, and #'x is read as (function x). So #'(lambda (...) ...) is read as (function (lambda (...) ...)), where function is the special operator in CL which says that its argument denotes a function;
  • lambda is defined as a macro: the expansion of (lambda (...) ...) is (function (lambda (...) ...): the identical form to the previous one.

This means that #'(lambda (...) ...), (lambda (...) ...) and (function (lambda (...) ...)) all are the same thing in CL: they all denote the function specified by (lambda (...) ...) in the current lexical environment.

There are two reasons why people might still use the #'(lambda (...) ...) version:

  • consistency – you need, for instance #'foo if foo is a function, so it may be seen as more consistent to use it for the lambda case too;
  • it was not always the case in CL that lambda had the macro definition it now does, so code that was written between the original CL specification and the ANSI standard, or which was written by people who remember those times, may use the #' form.