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

Double unquote in a macro

23 views
Skip to first unread message

Adam Warner

unread,
Oct 15, 2002, 1:32:24 AM10/15/02
to
Hi all,

This doesn't work because defparameter is a macro that doesn't evaluate
the second term of the input list to obtain the symbol name:

(defmacro grab-defun-name (list)
`(defparameter (second ,list) ,list))

I want to obtain the second value of the variable list which requires an
unquote on the list and an unquote on (second ,list)--which leads to too
many commas for one backquote.

I just want to understand whether achieving this is possible without
resorting to reimplementing defparameter or using EVAL. I believe I have
struck this situation in the FAQ:

http://www.faqs.org/faqs/lisp-faq/part3/

On the other hand, EVAL can sometimes be necessary when the only
portable interface to an operation is a macro.

I need to capture each function and store its list representation
(conveniently with the same name) along with evaluating the function
because I am simultaneous using the function and parsing its elements to
produce presentation markup.

Thanks,
Adam

Timothy Moore

unread,
Oct 15, 2002, 2:04:03 AM10/15/02
to
"Adam Warner" <use...@consulting.net.nz> writes:

> Hi all,
>
> This doesn't work because defparameter is a macro that doesn't evaluate
> the second term of the input list to obtain the symbol name:
>
> (defmacro grab-defun-name (list)
> `(defparameter (second ,list) ,list))
>
> I want to obtain the second value of the variable list which requires an
> unquote on the list and an unquote on (second ,list)--which leads to too
> many commas for one backquote.

Don't you just want
(defmacro grab-defun-name (list)
`(defparameter ,(second list) ,list))


>
> I just want to understand whether achieving this is possible without
> resorting to reimplementing defparameter or using EVAL. I believe I have
> struck this situation in the FAQ:
>
> http://www.faqs.org/faqs/lisp-faq/part3/
>
> On the other hand, EVAL can sometimes be necessary when the only
> portable interface to an operation is a macro.
>

I don't think this is one of those times.

Tim

Kenny Tilton

unread,
Oct 15, 2002, 2:21:37 AM10/15/02
to

Adam Warner wrote in message ...

>Hi all,
>
>This doesn't work because defparameter is a macro that doesn't evaluate
>the second term of the input list to obtain the symbol name:
>
> (defmacro grab-defun-name (list)
> `(defparameter (second ,list) ,list))
>
>I want to obtain the second value of the variable list which requires an
>unquote on the list and an unquote on (second ,list)--which leads to too
>many commas for one backquote.
>

howseabout `(defparameter ,(second list) ,list)? Then your problem may be
... well, I'll shut up. But I am surprised the defparam value ",list" is not
quoted: "',list". But I am only guessing at your intent. OK, I'll shut up.

kenny
clinisys


Adam Warner

unread,
Oct 15, 2002, 2:36:09 AM10/15/02
to
Hi Timothy Moore,

> Don't you just want
> (defmacro grab-defun-name (list)
> `(defparameter ,(second list) ,list))

Almost! Before the example I posted I was continually doing the same thing:

[1]> (defmacro grab-defun-name (list)
`(defparameter ,(second list) ,list))
grab-defun-name
[2]> (grab-defun-name '(defun test () 123))

*** - defparameter: non-symbol (defun test nil 123) cannot be a variable

Now I realise I was actually finding the second list value of
(quote (defun test nil 123))!

But now that I am no longer quoting the input value I need to add a quote
to the last list item:

(defmacro grab-defun-name (list)
`(defparameter ,(second list) ',list))

Now without quoting the input:

(grab-defun-name (defun test () 123)

The function is simultaneous captured and evaluted!

[7]> test
(defun test nil 123)
[8]> (test)
123

Regards,
Adam

Adam Warner

unread,
Oct 15, 2002, 2:46:01 AM10/15/02
to
Hi Kenny Tilton,

You wouldn't be surprised if you tried to get it to work using this input:

(grab-defun-name '(defun test () 123))

I had originally intended to create the global variable and then evaluate
the list. Impressively it all works by the macro capturing the defun list
before it evaluates.

Thanks,
Adam

Martin Simmons

unread,
Oct 15, 2002, 8:26:52 AM10/15/02
to
"Adam Warner" <use...@consulting.net.nz> wrote in message
news:pan.2002.10.15....@consulting.net.nz...

The defun won't be evaluated if you quote the list. Depending on how it will be
used, I would probably do it differently:

(defmacro defun-grab (name lambda-list &body body)
(let ((defun-form `(defun ,name ,lambda-list ,@body)))
`(progn
(defparameter ,name ',defun-form)
,defun-form)))

(defun-grab test2 () 123)

--
Martin Simmons, Xanalys Software Tools
zne...@xanalys.com
rot13 to reply

Adam Warner

unread,
Oct 15, 2002, 8:53:55 AM10/15/02
to
Hi Martin Simmons,

> The defun won't be evaluated if you quote the list. Depending on how
> it will be used, I would probably do it differently:
>
> (defmacro defun-grab (name lambda-list &body body)
> (let ((defun-form `(defun ,name ,lambda-list ,@body)))
> `(progn
> (defparameter ,name ',defun-form)
> ,defun-form)))
>
> (defun-grab test2 () 123)

This is much preferred thanks Martin. And it's not just because it will be
evaluated even if I quote the list: it's also a hassle closing the bracket
on the separate defun-grab.

Regards,
Adam

0 new messages