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

simple question about list arguments in macros

9 views
Skip to first unread message

jkc

unread,
Dec 14, 2009, 2:37:28 PM12/14/09
to
I'm puzzling over this. In this macro I can enter a list argument in
one of two ways and it works just fine:

(defmacro test (l)
`(progn
(format t "~&l=~s~%" ,l)))

(test '(1 2 3)) ; => l=(1 2 3)
(let ((lst '(4 5 6))) (test lst)) ; => l=(4 5 6)


But in this second example, it doesn't. Why is that?

(defmacro test2 (&key (l '(1 2 3)))
`(progn
(format t "~&l=~s~%" ,l)))

(let ((lst '(4 5 6))) (test2 :l lst)) ; => l=(4 5 6)
(test2 '(1 2 3))

1 compiler notes:

ex.lisp:15:1:
error:
(during macroexpansion of (TEST2 '(1 2 ...)))
error while parsing arguments to DEFMACRO TEST2:
odd number of elements in keyword/value list: ('(1 2 3))

Compilation failed.

There is something basic I don't understand here.

--jkc

Pascal J. Bourguignon

unread,
Dec 14, 2009, 2:48:09 PM12/14/09
to
jkc <jeffrey.k....@gmail.com> writes:

> I'm puzzling over this. In this macro I can enter a list argument in
> one of two ways and it works just fine:
>
> (defmacro test (l)
> `(progn
> (format t "~&l=~s~%" ,l)))
>
> (test '(1 2 3)) ; => l=(1 2 3)
> (let ((lst '(4 5 6))) (test lst)) ; => l=(4 5 6)
>
>
> But in this second example, it doesn't. Why is that?
>
> (defmacro test2 (&key (l '(1 2 3)))
> `(progn
> (format t "~&l=~s~%" ,l)))
>
> (let ((lst '(4 5 6))) (test2 :l lst)) ; => l=(4 5 6)
> (test2 '(1 2 3))

(test2 :L '(1 2 3))


> 1 compiler notes:
>
> ex.lisp:15:1:
> error:
> (during macroexpansion of (TEST2 '(1 2 ...)))
> error while parsing arguments to DEFMACRO TEST2:
> odd number of elements in keyword/value list: ('(1 2 3))
>
> Compilation failed.
>
> There is something basic I don't understand here.

Yes. The basic thing you missed in this case is DO NOT USE ONE-LETTER
VARIABLES!

If you have written: (defmacro test2 (&key (DATA-TO-DISPLAY '(1 2 3))) ...)

then you would have noticed that something was wrong in:
(test2 '(1 2 3)) ; vs.
(test2 :data-to-display my-data)


--
__Pascal Bourguignon__

Pascal J. Bourguignon

unread,
Dec 14, 2009, 2:50:26 PM12/14/09
to
jkc <jeffrey.k....@gmail.com> writes:
> There is something basic I don't understand here.

And to make Kenny happy, read:

http://smuglispweeny.blogspot.com/2008/07/aa-bb-cc-and-dd.html

--
__Pascal Bourguignon__

jkc

unread,
Dec 14, 2009, 3:00:45 PM12/14/09
to
On Dec 14, 11:48 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:

> If you have written: (defmacro test2 (&key (DATA-TO-DISPLAY '(1 2 3))) ...)
>
> then you would have noticed that something was wrong in:
> (test2 '(1 2 3)) ; vs.
> (test2 :data-to-display my-data)
>
> --
> __Pascal Bourguignon__


I can't believe I didn't notice that.
Sorry about the noise.

--jkc

jkc

unread,
Dec 14, 2009, 3:08:48 PM12/14/09
to
On Dec 14, 11:50 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> jkc <jeffrey.k.cunning...@gmail.com> writes:

Now that's an amusing read.

--jkc

jkc

unread,
Dec 18, 2009, 4:10:38 PM12/18/09
to

... and - I'm chagrined to say - after applying its philosophy to a
project I've been working on I discovered several bugs I didn't know
about. I'm a convert.

--jkc

0 new messages