Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Macro that writes lines of code

6 views
Skip to first unread message

Trastabuga

unread,
Oct 29, 2010, 12:38:51 PM10/29/10
to
Maybe it's a trivial thing, but I couldn't figure it out.
Let's say for some reason you need to write a macro that duplicates
some line of code.
For instance you want a macro for write-strings which duplicates a
write-string call as many times as the number of strings in the
parameters.
Certainly, there is a better solution for the problem, just to
illustrate the macro.

Thank you,
Andrei

Rainer Joswig

unread,
Oct 29, 2010, 1:01:49 PM10/29/10
to
In article
<6dc2d1f6-71d9-4529...@t13g2000yqm.googlegroups.com>,
Trastabuga <lisp...@gmail.com> wrote:

Remember, programs are Lisp data - not lines of text.
You can manipulate the programs with ordinary functions
like MAPCAR and LIST.


(defmacro write-strings (&rest strings)
`(progn
,@(mapcar (lambda (string)
(list 'write-string string))
strings)))

CL-USER 4 > (macroexpand '(write-strings "foo" "bar" "baz"))
(PROGN (WRITE-STRING "foo") (WRITE-STRING "bar") (WRITE-STRING "baz"))
T

0 new messages