Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, note that</p> <pre><code>`(concat ,(symbol-name a)) </code></pre> <p>and</p> <pre><code>(list (quote concat) (symbol-name a)) </code></pre> <p>do the exact same thing. They are equivalent pieces of code (backquote syntax isn't restricted to macro bodies!): Both construct a list whose first element is the symbol <code>CONCAT</code> and whose second element is the symbol name of whatever the variable <code>A</code> refers to.</p> <p>Clearly, this only makes sense if <code>A</code> refers to a symbol, which, as Svante has pointed out, isn't the case in the macro call example.</p> <p>You <em>could</em>, of course, extract the symbol from the list <code>(QUOTE FOO)</code>, but that prevents you from calling the macro like this:</p> <pre><code>(let ((x 'foo)) (tm x)) </code></pre> <p>which raises the question of why you would event want to force the user of the macro to explicitly quote the symbol where it needs to be a literal constant anyway.</p> <p>Second, the way macros work is this: They take pieces of code (such as <code>(QUOTE FOO)</code>) as arguments and produce a new piece of code that, upon macroexpansion, (more or less) replaces the macro call in the source code. It is often useful to reuse macro arguments within the generated code by putting them where they are going to be evaluated later, such as in</p> <pre><code>(defmacro tm2 (a) `(print (symbol-name ,a))) </code></pre> <p>Think about what this piece of code does and whether or not my <code>let</code> example above works now. That should get you on the right track.</p> <p>Finally, a piece of advice: Avoid macros when a function will do. It will make life much easier for both the implementer and the user.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload