[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [O] Why does evaluating a piece of Elisp code seemingly not expand a
From: |
Marcin Borkowski |
Subject: |
Re: [O] Why does evaluating a piece of Elisp code seemingly not expand a macro? |
Date: |
Sun, 17 Jan 2016 23:56:49 +0100 |
User-agent: |
mu4e 0.9.13; emacs 25.1.50.1 |
On 2016-01-15, at 11:57, Oleh Krehel <address@hidden> wrote:
> Marcin Borkowski <address@hidden> writes:
>
>> Why?
>
> Macro-expand the defun to get:
>
> (defalias 'print-answer
> #'(lambda nil
> (message
> "The answer is %s."
> (forty-two))))
>
> `lambda' is a macro that /quotes/ its body. Therefore, the body of
> `defun' is not evaluated or expanded when it's defined.
Interesting.
1. Why is lambda sharp-quoted? I remember reading (in Artur's blog)
that it shouldn't be.
2. I always thought that macros get expanded on compilation (or defining
the function). If I evaluate all forms I've written about outside Org
(using C-M-x, for instance), the `forty-two' macro seems to get
expanded.
In the manual (info "(elisp)Expansion"), I could find this:
--8<---------------cut here---------------start------------->8---
Note that Emacs tries to expand macros when loading an uncompiled
Lisp file. This is not always possible, but if it is, it speeds up
subsequent execution. *Note How Programs Do Loading::.
--8<---------------cut here---------------end--------------->8---
Does it mean that C-M-x is different than loading? Or C-x C-e, for that
matter? Is this covered by the manual? (If not, it might need
correcting.)
> You probably wanted something like this instead:
>
> (macroexpand-all
> '(lambda nil
> (message
> "The answer is %s."
> (forty-two))))
> ;; =>
> ;; (function
> ;; (lambda nil
> ;; (message
> ;; "The answer is %s."
> ;; 42)))
>
> Which could be wrapped in a new macro:
>
> (defmacro defun-1 (name arglist &optional docstring &rest body)
> (unless (stringp docstring)
> (setq body
> (if body
> (cons docstring body)
> docstring))
> (setq docstring nil))
> (list 'defun name arglist docstring (macroexpand-all body)))
>
> The above seems to work, at least superficially:
>
> (symbol-function
> (defun-1 print-answer ()
> (message "The answer is %s." (forty-two))))
> ;; =>
> ;; (lambda nil
> ;; (message
> ;; "The answer is %s."
> ;; 42))
Interesting, I will study this (but not today - it's 23:51 here, I'll
need sleep soon!)
> By the way, it might be more appropriate to ask similar questions on
> address@hidden
I posted this reply there, too, though in view of what I wrote above
I still think this is Org-related.
> Oleh
Best,
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University
Re: [O] Why does evaluating a piece of Elisp code seemingly not expand a macro?, Samuel W. Flint, 2016/01/15