Re: macro that writes macros



På Sun, 30 Dec 2007 03:13:39 +0100, skrev <jordi.burguet.castell@xxxxxxxxx>:

And it works. But then I thought about combining all those "(new-
tag ...)" like this:

(defmacro new-tags (&rest tags)
(dolist (tag tags)
`(new-tag ,tag)))

(new-tags html head title body p)

But it doesn't work! I tried without the "`" nor the "," in the "new-
tags" definition and it doesn't work either. I have been fighting with
this simple-looking problem for a long time, reading docs and stuff,
and I can't make it work anyway nor see what is wrong! Anyone has some
advice on what's going on or what I should check?


Well if you try to macroexpand this you will notice that the macro return nil.
This is because although dolist iterates over the list it don't return the elements.

(defmacro new-tags (&rest tags)
(let ((defs (list 'progn)))
(dolist (tag tags)
(push `(new-tag ,tag) defs))
(nreverse defs)))

CL-USER 3 > (pprint (macroexpand '(new-tags html head title body p)))

(PROGN
(NEW-TAG HTML)
(NEW-TAG HEAD)
(NEW-TAG TITLE)
(NEW-TAG BODY)
(NEW-TAG P))

which is probably what you want.

--------------
John Thingstad
.



Relevant Pages