Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can definitely write a macro which uses itself in its own expansion. This makes perfect sense, and it is a natural way to write the COND macro, which has a regular, expanding structure due to the arbitrarily long list of cond pairs, that can be expressed by the usual car/cdr or first/rest recursion.</p> <pre><code>(defmacro new-cond (&amp;rest cond-pairs) (cond ;; you need cond to compile new-cond syntax, LOL! ((null cond-pairs) nil) ((atom cond-pairs) (error "new-cond: bad syntax!")) (t `(if ,(first (first cond-pairs)) (progn ,@(rest (first cond-pairs))) (new-cond ,@(rest cond-pairs)))))) &gt; (macroexpand-1 '(new-cond (1 2) (3 4))) (IF 1 (PROGN 2) (NEW-COND (3 4))) </code></pre> <p>This is very analogous to lazy list processing. <code>macroexpand</code> expands just the outer macro, leaving a "promise" object to continue the expansion. That "promise" object is the macro call <code>(NEW-COND (3 4))</code> at the tail.</p> <p>Of course the real macro expander will walk the whole form and "force" all these promises (unexpanded macro calls) until no more remain.</p> <p>I think there are some advantages to this style, such as easy debugging with <code>macroexpand</code>. You get a small expansion. If its recursive nature is obvious, that's a win. If the macro is such that your brain needs to see the whole thing expanded, it's a loss.</p> <p>Here, I can see that <code>(IF 1 (PROGN 2) (NEW-COND (3 4)))</code> is the correct compile. If 1 is true, evaluate the forms list (2). Otherwise keep going with the other cond pairs. Now we have to verify the one pair case:</p> <pre><code>&gt; (macroexpand-1 '(new-cond (3 4))) (IF 3 (PROGN 4) (NEW-COND)) </code></pre> <p>Excellent, and the no-pair case <code>(NEW-COND)</code>, by obvious inspection, reduces to nil.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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