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

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

Re: understanding backquote


From: Pascal J. Bourguignon
Subject: Re: understanding backquote
Date: Tue, 02 Jun 2015 22:03:58 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Olaf Rogalsky <olaf.rogalsky@aol.de> writes:

> Hi,
>
> I have problems in understanding the semantics of backquote. Consider
> the following function:
>
> (defun test () `,(* (+ 1 2) (+ 3 4)))
>
> Now let's have a look at the defintion of the symbol:
>
> (symbol-function 'test)
> => (lambda nil (* (+ 1 2) (+ 3 4))) 
>
> Huhh??? I was thinking, that the above definition is equivalent to the
> following:
>
> (defun test () 21)
>
> Ok, this was unexpected, but perhaps the compiler does it "right":
> (disassemble (byte-compile 'test))
> =>  byte code:
>       args: nil
>     0       constant  21
>     1       return    
>
> Yes, it does. It is only, that I suspect, that this has nothing to do
> with backquotes, but rather with constant folding optimization.
>
> Can anybody explain this to me?

`,x is equivalent to x.



(info "(elisp) Backquote")

More details are given in the Common Lisp specification of the backquote
reader macro, but it's "extrapolation" relative to emacs lisp:
http://www.lispworks.com/documentation/HyperSpec/Body/02_df.htm



Now, the fact that you obtain a simple constant function is unrelated
to the backquote, but comes from the fact that you have a good and smart
compiler, that notices that the expression is a constant expression, and
therefore it is reduced at compilation time:

    (disassemble (byte-compile (lambda nil (* (+ 1 2) (+ 3 4)))))
    byte code:
      args: nil
    0       constant  21
    1       return    


With variables, it is another matter:

    (disassemble (byte-compile (lambda (a b c d) `,(* (+ a b) (+ c d)))))
    byte code:
      args: (a b c d)
    0       varref    a
    1       varref    b
    2       plus      
    3       varref    c
    4       varref    d
    5       plus      
    6       mult      
    7       return    


-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


reply via email to

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