all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: Purpose of dash # in elisp
Date: Mon, 09 Nov 2009 14:00:33 +0100	[thread overview]
Message-ID: <87k4xz7szy.fsf@galatea.local> (raw)
In-Reply-To: mailman.10334.1257764064.2239.help-gnu-emacs@gnu.org

Tassilo Horn <tassilo@member.fsf.org> writes:

> Nordlöw <per.nordlow@gmail.com> 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__


  parent reply	other threads:[~2009-11-09 13:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-08 20:01 Purpose of dash # in elisp Nordlöw
2009-11-08 21:29 ` Pascal J. Bourguignon
2009-11-09 10:53 ` Tassilo Horn
     [not found] ` <mailman.10334.1257764064.2239.help-gnu-emacs@gnu.org>
2009-11-09 13:00   ` Pascal J. Bourguignon [this message]
2009-11-09 17:27     ` Lennart Borgman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k4xz7szy.fsf@galatea.local \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.