Basically, when should I use Emacs Lisp's function
procedure? I haven't found any examples in which there's a difference in behavior if you pass functions as arguments 'like-this
or #'like-this
. In fact, if I evaluate (eq 'goto-char #'goto-char)
it returns t
.
The Emacs Lisp code that I've come across rarely uses function
/#'
; the authors just quote
/'
everything.
Example: (add-hook 'emacs-lisp-hook 'turn-on-eldoc-mode)
However, I can find a few counterexamples. Here's one from the source code of Emacs 24.3's electric.el
:
(add-hook 'post-self-insert-hook #'electric-indent-post-self-insert-function 'append)
(do-something '(lambda …
(do-something (lambda …
'
and #'
as long as I'm using a version of Emacs more recent than X?Why Emacs? Emacs helps you be productive by providing an integrated environment for many different kinds of tasks: All of the basic editing commands (and there are lots of them) are available no matter what you're trying to do: write code, read a manual, use a shell, or compose an email.
Emacs tends to be relatively straightforward, similar to commonly used text editors like Notepad. On the other hand, Vim is a power-user's tool, using keyboard shortcuts to speed up tasks. Vim is known to have a much steeper learning curve than Emacs.
Emacs is a text editing tool that comes out-of-the-box with Linux and macOS. As a (less popular) cousin of Vim, Emacs also offers powerful capabilities with easy-to-install language support, and can even help you navigate faster in macOS with the same keybindings.
Emacs is a Text Editor which can morphed into any tool. Everything can be done inside it and can be done using only keyboard.
function
(aka #'
) is used to quote functions, whereas quote
(aka '
) is used to quote data. Now, in Emacs-Lisp a symbol whose function cell is a function is itself a function, so #'symbol
is just the same as 'symbol
in practice (tho the intention is different, the first making it clear that one is not just talking about the symbol "symbol" but about the function named "symbol").
The place where the difference is not just stylistic is when quoting lambdas: '(lambda ...)
is an expression which evaluates to a list whose first element is the symbol lambda
. You're allowed to apply things like car
and cdr
to it, but you should not call it as if it were a function (although in practice it tends to work just fine). On the contrary #'(lambda ...)
(which can be written just (lambda ...)
) is an expression which evaluates to a function. That means you can't apply car
to it, but the byte-compiler can look inside #'(lambda ...)
, perform macro-expansion in it, warn you if what it finds doesn't look kosher, etc...; For lexical-binding it even has to look inside in order to find the free variables to which that function refers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With