Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use #' before function arguments in emacs-lisp?

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.

like image 396
seanmcl Avatar asked Aug 22 '13 20:08

seanmcl


People also ask

Which is why use in sentence?

Which is why is used to introduce a subordinate clause (one that does not form a sentence by itself): Motorcycles are dangerous, which is why we should wear helmets. The company went bankrupt, which is why their new building is unfinished. The sunsets here are beautiful, which is why many artists come to paint them.

Why do we use in English?

We use of when we want to show that people or things relate to other things or people. For example, when we want to say that something or someone belongs to or is a part of something or someone else, we can do it like this: Tiffany stared at the floor of her room.

Why used to is used?

We use used to + infinitive to talk about a past situation that is no longer true. It tells us that there was a repeated action or state in the past which has now changed.

Why is and/or used?

And/or (sometimes written and or) is an English grammatical conjunction used to indicate that one or all). It is used as an inclusive or (as in logic and mathematics), because saying "or" in spoken language (or writing "or") might be inclusive or exclusive.


1 Answers

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.

like image 172
Svante Avatar answered Sep 24 '22 03:09

Svante