1

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

Vroomfondel
  • 2,704
  • 1
  • 15
  • 29
  • Note that there is a difference when you reference a function by name, between `#'foo` and `foo`: see https://lispcookbook.github.io/cl-cookbook/functions.html#referencing-functions-by-name-single-quote--or-sharpsign-quote- – Ehvince Dec 14 '20 at 12:40
  • Also: [Why sharp quote lambda expressions?](https://stackoverflow.com/questions/29338004/why-sharp-quote-lambda-expressions) – ad absurdum Dec 14 '20 at 14:20
  • This is one of the flaws of the book. Common Lisp provides a nice `lambda` macro which translates `(lambda ...)` into `(function (lambda ...))`. It behooves us to use it instead of proliferating extraneous `#'` syntax – Kaz Dec 14 '20 at 19:13

1 Answers1

4

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.
  • There is no good reason to use `#'(lambda ...)`. It is pure verbiage, and it is a blemish upon the PCL book to be doing so. `(lambda ...)` is not the name in the function namespace; it is no more consistent to apply `#'` to it than to any other operator that might calculate a function as its result. E.g. if we have `(op + 3)` to partially apply `+`, it would not be "consistent" to have it callable as `#'(op + 3)`. It would just be a sick joke. – Kaz Dec 14 '20 at 19:15
  • 1
    @Kaz: I didn't say there are *good* reasons: I said there are reasons, and those reasons certainly once were good. Certainly I am not about to make a gratuitous change touching hundreds of lines (maybe thousands, because I have to reindent everything) of code I wrote in the days when you could not portably assume that `lambda` was a macro (and yes: I have significant amounts of such code still). –  Dec 15 '20 at 08:50