0

I am trying to learn Elisp, so I am reading the GNU Manual for Elisp. Everything so far is easy to understand, however when I read to the section of macro I encountered something I have a hard time understanding. I couldn't really find an adequate explanation neither:

For example, a simple macro program that increments the variable by 1:

(defmacro inc (var)
   (list 'setq var (list '1+ var)))

I am not sure why there is ' symbol in front of setq and 1+ ? Won't this make them to a list of literal elements? (For example a list containing three elements (setq var (1+ var))

Why can't I write:

; this seems more reasonable to me
(defmacro inc (var)
   (setq var (1+ var))

I am not really sure how list works here and it seems strange to me using list here. Can someone explain to me?

Yan Zhuang
  • 436
  • 3
  • 10
  • I began typing in a rather detailed answer to your previous question, but you deleted it before I got a chance to finish it. – tripleee May 09 '21 at 19:42
  • Oh I am so sorry, I was thinking that I am going to clarify my question by asking a simpler but more precise question. Will it help if I undelete it ? :( – Yan Zhuang May 09 '21 at 19:44
  • 1
    The macro chapter does explain all of this. In very brief, you can write `(defmacro inc (var) \`(setq ,var (1+ ,var)) )`; look up the documentation for this other quoting mechanism. – tripleee May 09 '21 at 19:44
  • Yes I understand this quoting mechanism, but my problem is that I don't understand why do I have to evaulate like a "string"? Why I can't write a macro like I wrote a normal function, just `setq var (1+ var)` ? – Yan Zhuang May 09 '21 at 19:46
  • 1
    I'll have to run so probably not worth undeleting now. If you are available some 12 hours from now we can perhaps try to coordinate. But the gist of it was, look at the Emacs Lisp Reference Manual. – tripleee May 09 '21 at 19:46
  • Okay okay sure sure thanks~ – Yan Zhuang May 09 '21 at 19:47
  • 1
    The point of macros is that they allow you to shape the syntax of the language in a way that functions cannot. The cost you pay is that they require more complex quoting. – tripleee May 09 '21 at 19:47
  • Ah okay I understand now :) – Yan Zhuang May 09 '21 at 19:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232158/discussion-between-tripleee-and-yan-zhuang). – tripleee May 10 '21 at 08:26
  • Cross-referencing with your other question https://stackoverflow.com/q/67461039 – phils May 10 '21 at 12:14

1 Answers1

3

Won't this make them to a list of literal elements? (For example a list containing three elements (setq var (1+ var))

Precisely so (if we substitute the actual argument for var).

A macro generates/returns code.

This is the "expansion" phase of the macro, which typically takes place during byte-compilation of the elisp file.

So as far as the contents of that byte-compiled .elc file are concerned, there's no difference between you having used either of these things in your .el source file:

  • (inc foo)
  • (setq foo (1+ foo))

I.e. In both cases the code which gets byte-compiled is (setq foo (1+ foo))

Outside of byte-compilation, a macro might be expanded when the .el file is loaded or, failing that, on-demand right before the expanded code needs to be evaluated (but you should always assume the expansion happens completely independently of the subsequent evaluation).

phils
  • 71,335
  • 11
  • 153
  • 198