1

Are 'function-name and (function function-name) completely equivalent, or are there conditions under which I must use the more verbose (function function-name)?

(apply 'message ( list "call func via quote"))
(apply (function message) (list "call func via function keyword"))

Both of them yield the same effect. But in many elisp libraries, i am seeing people using (function func) to pass function symbols like in this example.

(defun dired-add-file (filename &optional marker-char)
  (dired-fun-in-all-buffers
   (file-name-directory filename) (file-name-nondirectory filename)
   (function dired-add-entry) filename marker-char))

Why do people use more verbose way, when they can use just a quote?

  • I rephrased the question somewhat so that it would not get dinged for subjectivity. If I misunderstood your intent, we can roll back the edit. – Dan Sep 22 '16 at 0:41
  • This is a nice explanation on the difference between #' (aka function) and ': endlessparentheses.com/…. Basically, as C-h f function says: "In byte compilation, function causes its argument to be compiled. quote can not do that. IMHO, just use #' rather than '. – Omair Majid Sep 22 '16 at 1:08
3

'foo and (quote foo) are identical: they prevent evaluation of foo.

#'foo and (function foo) are also identical: they extract the function binding of foo, including at compile time, which means that the function gets compiled.

So, if you want a symbol, use 'foo:

(make-variable-buffer-local 'my-local-var)

if you want a function, use #'foo:

(setq my-after-function #'my-function)

or

(apply #'my-function arg1 arg2 others)

See also

  1. When to use 'quote in Lisp
  2. Why sharp quote lambda expressions?
  • #'foo and (function foo) don't extract the function binding, they just return a symbol. Just try this to get convinced: (type-of (function type-of)) (type-of (symbol-function (function type-of))) – memeplex Nov 29 '18 at 2:55
  • @memeplex: only in Emacs Lisp and only at this time. Things may change. ;-) – sds Nov 29 '18 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.