Why use #' before function arguments in emacs-

2019-04-04 03:42发布

问题:

I'm familiar with Emacs Lisp, but not Common (or any other) Lisp. Some Lisp programmers suggest (e.g. A basic function for emacs) that it's good to use #' in front of function arguments in Lisp code. For example:

(mapc #'my-fun '(1 2 3))

In Emacs Lisp, I believe that this is equivalent to

(mapc 'my-fun '(1 2 3))

From the elisp manual, section 12.7.

The read syntax #' is a short-hand for using function. The following forms are all equivalent:

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

and the help for function

function is a special form in eval.c.

(function ARG)

Like quote, but preferred for objects which are functions. In byte compilation, function causes its argument to be compiled. quote cannot do that.

So it seems like a potential optimization, but no more. Moreover, coming from an ML/Haskell background, it seems strange to treat a function differently from any other data.

Question:

Do you agree that #' should be used in emacs-lisp function arguments? (A brief explanation of why they're needed in Common Lisp would be great as well.)

Notes:

I thought it may read a different cell when the #' is omitted (value vs function). But this seems wrong because the documentation for function doesn't say anything about grabbing the function cell. This is achieved by using symbol-function.

Related questions are

  • The #' in common lisp
  • Writing lambda expressions in common lisp
  • Why #' is used before lambda in Common Lisp?

but they seem to suggest that the #' is unnecessary, at least in lambdas.

回答1:

The quote character in #'foo has nothing to do with the one in 'foo.

#'foo is replaced at read time by (function foo). When that is compiled and executed, it looks up the functional definition named by foo (by defun, flet, labels or similar).

'foo is replaced at read time by (quote foo). When that is compiled and executed, it is simply replaced by the symbol foo.

Funcall and apply (and thus generally higher order functions) take function designators as arguments. A function designator can be a function or a symbol naming a function, so both #'foo and 'foo are function designators.

Therefore, the forms 'foo and #'foo seem interchangable at first glance. However, the lookup of the real function is done at different times—#'foo is looked up where it is invoked, while the function named by 'foo would only be looked up when it is finally applied.

If you use a function designator many times over, it is much more efficient to do the lookup just once, or even just at compile time. That can be a really big timesaver, and translates to a smoother user experience in an editor.