help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Purpose of dash # in elisp


From: Pascal J. Bourguignon
Subject: Re: Purpose of dash # in elisp
Date: Mon, 09 Nov 2009 14:00:33 +0100
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

Tassilo Horn <address@hidden> writes:

> Nordlöw <address@hidden> writes:
>
>> What purpose does # as a quote suffix?
>
> You mean as prefix, e.g.
>
>     (mapcar #'oddp '(1 2 3))
>
> versus
>
>     (mapcar 'oddp '(1 2 3))?
>
> Both forms are completely equivalent (try disassembling them), but the
> former is more common to a common lisp programmer, where you pass
> function symbols with the `function' macro and its reader form #', and
> other symbols that should not be evaled with `quote' (which is ').
>
> If you stick to the convention to always use #' when passing a function
> as arguments, the code might be a littlebit more explanatory, because
> then you can see that a function is passed on a first glance.  On the
> other hand, in elisp the reader won't error or warn if you use #' to
> pass anything different, so this convention wouldn't be enforced
> somehow.

(first ' #'x) --> function
(first '  'x) --> quote

It happens that in emacs lisp, function and quote evaluate the same:
they both return their unevaluated argument unchanged.

However, when compiling, they don't behave the same.  function means
that the argument is CODE, while quote says that the argument is DATA.
 
(disassemble (byte-compile (lambda ()  '(lambda (x) (+ x x)))))
produces:

byte code:
  args: nil
0       constant  (lambda (x) (+ x x))
1       return    



(disassemble (byte-compile (lambda ()  #'(lambda (x) (+ x x)))))
produces:

byte code:
  args: nil
0       constant  <compiled-function>
      args: (x)
    0       varref    x
    1       dup       
    2       plus      
    3       return    

1       return    


When the lambda form is quoted, it is considered data, and the
compiler doesn't compile it, but when it's function'ed, it is
considered code, and it is compiled too.

Notice that lambda itself is a macro that expands to a function form:

(macroexpand '(lambda (x) (+ x x)))
--> (function (lambda (x) (+ x x)))

Nonetheless some programmers (not me) like to prefix lambda forms with #':

   (mapcar #'(lambda (x) (+ x x)) '(1 2 3))

I just write:

  (mapcar (lambda (x) (+ x x)) '(1 2 3))

which compiles to the same.  On the other hand,  compiling:

  (mapcar '(lambda (x) (+ x x)) '(1 2 3))

wouldn't compile the lambda form, and therefore would produce slower
code in general.


In the case of function names, 'fun or #'fun will indeed produce the
same results in all cases, in emacs lisp (in Common Lisp they would
produce different results in some situations). 

emacs lisp:

    'sin  --> sin
    #'sin --> sin

Common Lisp:

    'sin  --> SIN
    #'sin --> #<SYSTEM-FUNCTION SIN>

-- 
__Pascal Bourguignon__


reply via email to

[Prev in Thread] Current Thread [Next in Thread]