7

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.

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.

The lambda form has one other effect: it tells the Emacs evaluator and byte-compiler that its argument is a function, by using function as a subroutine.
[...] The following forms are all equivalent:

(lambda (x) (* x x)) 
(function (lambda (x) (* x x))) 
#'(lambda (x) (* x x))

This renders the function form useless in this case.

Is there any other situation where the function form would have been useful?
Is there any case when its presence is neither unnecessary nor identical to quote?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Malabarba
  • 4,473
  • 2
  • 30
  • 49
  • Note, I'm aware of this related question. http://stackoverflow.com/q/1852844/491532 – Malabarba Aug 12 '14 at 23:45
  • Note that `(lambda ...)` was not *always* equivalent to `(function (lambda ...))`, and therefore one answer to your question is "yes, in sufficiently old versions of Emacs". – phils Aug 13 '14 at 10:20
  • It's no longer necessary to do (function (lambda ...)) as opposed to (lambda ...). But the first quote compares it with `quote`. You should prefer `(funcall (lambda ...) ...)` or `(funcall (function (lambda ...)) ...)` over `(funcall (quote (lambda ...)) ...)`. In the former, you get a compiled function; in the latter you have a literal list. – Joshua Taylor Aug 13 '14 at 12:22
  • @phils: `lambda` was added as a macro that wraps itself in a `function` in 1992 (by Jim Blandy), i.e. for Emacs-19. So, while the equivalence wasn't true in Emacs-18, that's a rather long time ago. I still have Emacs-19 working here, but I don't have an Emacs-18 binary. – Stefan Aug 13 '14 at 19:23
  • Thanks, Stefan. I was sure that "sufficiently old" would prove to be much older than anyone had any practical reason to worry about, but it's always interesting to hear the exact details :) (my comment was just to point out that once upon a time there was indeed a difference). – phils Aug 13 '14 at 21:49

3 Answers3

11

#' (aka function) is used internally by lambda, since lambda is defined as a macro that wraps itself in a function. But other than that, you indeed can write pretty much any Elisp code without using #'. There is one other subtlety, tho: if you write #'foo you tell Emacs that you think foo is the name of a function, so recent versions of the byte-compiler will then warn if foo is not a known function (just like they've been warning about calls to (foo ...) for many years already).

As pointed out by @Bruce, the use of #'foo instead of 'foo also makes a real difference for functions defined locally via cl-flet or cl-labels: in that case #'foo refers to the locally defined function, whereas 'foo will simply refer to the foo symbol, which is not actually related to the local function definition and may hence by unbound or bound to another function:

(cl-flet ((a () 1)) (list 'a (functionp 'a) #'a))

returns

(a nil (lambda nil 1))
Stefan
  • 27,908
  • 4
  • 53
  • 82
  • This commenter on reddit found another difference. Function does lexical binding. http://www.reddit.com/r/emacs/comments/2ddwgc/does_the_function_form_serve_any_actual_purpose/cjooj19 – Malabarba Aug 13 '14 at 11:34
  • Indded, Bruce, I had forgotten about that, but the `#'` is needed to refer to local functions defined via `cl-flet` and `cl-labels`. – Stefan Aug 13 '14 at 11:55
  • -1 This explains the (non-)difference between (lambda ...) and (function (lambda ...)), but it doesn't touch at all on the first part of the question. In Emacs, you can still do `(funcall (quote (lambda ...)) ...)`, and it's in that context that you won't be compiling the function form as a function, but rather as a list. It's not that (function (lambda ...)) is preferred over (lambda ...), but that (function (lambda ...)) is preferred over (quote (lambda ...)). – Joshua Taylor Aug 13 '14 at 12:20
  • 1
    I don't understand what "first part of the question" you're referring to. But indeed, there's no preference of `#'(lambda ...)` over just `(lambda ...)`. – Stefan Aug 13 '14 at 13:11
  • @Stefan, the first quotation in the question says that function is **"Like `quote`, but preferred for objects which are functions. In byte compilation, `function` causes its argument to be compiled. `quote` cannot do that."** The title of the question is **Does (function) serve any purpose in Emacs?** The answer is *yes* (no disagreement there), and it's even used in the expansion of macro `(lambda …)` to `(function (lambda …))`. Because the first quotation from the documentation contrasts it with **quote**, the real issue here is not between `(lambda …)` and `(function (lambda …))`, since… – Joshua Taylor Aug 13 '14 at 14:12
  • …the former expands to the latter, but rather between `(quote (lambda …))` and `(function (lambda …))`. In Emacs, you can still funcall a list beginning with lambda (e.g., `(funcall '(lambda (x) x) 2) ;=> 2`), but you won't get any compilation. *That's* where the benefit of `(function …)` (or the shorthand `#'…`) comes in. – Joshua Taylor Aug 13 '14 at 14:14
  • Yes, we all violently agree on this. I see nowhere in this page where someone argues about "(lambda …) vs (function (lambda …))". – Stefan Aug 13 '14 at 15:52
  • @Stefan I think I may have been unclear in the comments. I've tried to make a clearer explanation in [an answer](http://stackoverflow.com/a/25285974/1281433). (I've also removed my -1; I think having the information available in another answer will let others see if sufficiently.) – Joshua Taylor Aug 13 '14 at 16:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59299/discussion-between-stefan-and-joshua-taylor). – Stefan Aug 13 '14 at 20:11
4

lambda is a macro. It has to be expanded and it is expanded every time. Like this:

(lambda (x) x) -> (function (lambda (x) x)) ; #'(lambda (x) x)

function is a special operator. In fact, it treats lambda forms specially, otherwise we would never be able to expand lambda macro:

(lambda (x) x) ->
  (function (lambda (x) x)) ->
    (function (function (lambda (x) x))) ->
      ...

function looks at its argument and if it is a lambda form, it compiles the function. If its argument is a symbol, then it searches for compiled function associated with the symbol.

So, we see now, that function (like quote) does not evaluate its argument. It must have special behavior, thus it is a special operator.

Mark Karpov
  • 7,499
  • 2
  • 27
  • 62
  • Technically, function doesn't compile anything, it enables the compilation. I asked this question because lambda by itself already enables compilation, and I'm not aware of any case where the compiler actually compiles symbols. – Malabarba Aug 13 '14 at 11:37
  • @Bruce, well, when applied to lambda, `function` returns lexical closure. Compiled it or not is implementation dependent. For example in SBCL [everything is compiled](http://www.sbcl.org/manual/#Compiler_002donly-Implementation). Every closure. As for Emacs Lisp, I really don't know how it works. I used word 'compile' for simplification. – Mark Karpov Aug 13 '14 at 11:54
  • I see your point. I was referring to emacs lisp, where (lambda) is perfectly identical to (function (lambda)). But I see the value in your answer now. – Malabarba Aug 13 '14 at 12:09
  • @BruceConnor The first quotation in the question is the key to where emacs can work with code that *definitely* won't be compiled. You can (in Emacs, but not in CL) `(funcall '(lambda (x) x) 2)`, for instance. Since you can't do that in most Lisps anymore, the difference isn't so marked, but the important difference here is between `(quote (lambda …))` and `(function (lambda …))`, not between `(function (lambda …))` and `(lambda …)` (since the latter just expands to the former). – Joshua Taylor Aug 13 '14 at 14:17
4

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.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Indeed. This is discussed also at http://stackoverflow.com/questions/16801396/when-should-emacs-function-syntax-be-used/16802304#16802304 – Stefan Aug 13 '14 at 19:05
  • @Stefan Now that's a nice answer that discusses this issue. I'll gladly +1 your answer here if you touch on that point. I think it's the important part in answering whether "(function) serve any purpose in Emacs?" If you couldn't funcall a list, then **(lambda …)** would already have to be something akin to **(function (lambda …))**. It's only really needed (aside from local functions, a la flet/labels) because there's an alternative with quote. – Joshua Taylor Aug 13 '14 at 19:14