Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with macro behaviour in lisp
    primarykey
    data
    text
    <p>If in the REPL I do this:</p> <pre><code>(dolist (x (1 2 3)) (print x)) </code></pre> <p>then I get an error since in (1 2 3) the digit 1 is not a symbol or a lambda expr. If I do:</p> <pre><code>(dolist (x (list 1 2 3)) (print x)) </code></pre> <p>then it works ok.</p> <p>My question is why the following works:</p> <pre><code>REPL&gt; (defmacro test (lst) (dolist (x lst) (print x))) =&gt; TEST REPL&gt; (test (1 2 3)) 1 2 3 =&gt;NIL </code></pre> <p>Why does dolist accept (1 2 3) when it is inside the macro definition but not when directly in the repl? The assumption: </p> <p>"Since TEST is a macro ,it does not evaluate its arguments, so (1 2 3) is passed as is to the dolist macro. So dolist must complain like it does when it is passed (1 2 3) in the REPL" </p> <p>is obviously wrong. But where?</p> <p>UPDATE: Although the answers help clarify some misunderstandings with macros, my question still stands and i will try to explain why:</p> <p>We have established that dolist evaluates its list argument(code blocks 1, 2). Well, it doesnt seem to be the case when it is called inside a macro definition and the list argument that is passed to it is one of the defined macro arguments(code block 3). More details: A macro, when called, does not evaluate its arguments. So my test macro, when it is called, will preserve the list argument and will pass it as it is to the dolist at expansion time. Then at expansion time the dolist will be executed (no backquotes in my test macro def). And it will be executed with (1 2 3) as argument since this is what the test macro call passed to it. So why doesnt it throw an error since dolist tries to evaluate its list argument, and in this case its list argument (1 2 3) is not evaluatable. I hope this clears my confusion a bit.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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