So one would do #'(lambda ...)
to enable byte compilation of the
lambda form.
On the other hand, as mentioned in the manual, That is no longer
necessary.
It's no longer necessary for the programmer to write (function (lambda …)) (or the shorthand #'(lambda …), because (lambda …) expands to (function (lambda …)). Other answers have explained that very well. You first cite the documentation for function:
From the documentation of the function form:
Like quote
, but preferred for objects which are functions. In byte
compilation, function
causes its argument to be compiled. quote
cannot do that.
Thus, the documentation of function isn't addressing the difference between
(function (lambda …))
and
(lambda …)
,
but rather between
(quote (lambda …))
and
(function (lambda …))
.
In most modern Lisps (Common Lisp, Scheme, etc.), the form (quote (lambda …)), or simply '(lambda …)) is just a list, and it's not something that can be invoked. E.g., in SBCL:
* (funcall '(lambda () 42))
; debugger invoked on a TYPE-ERROR in thread #<THREAD "initial thread" RUNNING {1002978E71}>:
; The value (LAMBDA () 42) is not of type (OR FUNCTION SYMBOL).
However, in Emacs Lisp, you can invoke a list:
(funcall '(lambda () 42))
;=> 42
In asking whether function serves any purpose, we have to ask "what can we do without it," or "what alternative is there?" We can't respond that that "we would just write (lambda …)," because, as others have pointed out, that just expands to (function (lambda …)). If we don't have function, we can still use quote. We could still write a lambda macro that expands to (quote (lambda …)), and thus write (funcall (lambda …)), and the code would look the same. The question is, "what's the difference?" The difference is that in a quote-based version, we're passing literal lists around, and these can't get compiled to functions because we still have to be able to do list-like things with them (e.g., take their car and cdr). This is where function is useful, whether we write it ourselves, or depend on a macro to use it in an expansion. It gives a way of writing function objects instead of lists.