From: Adam Warner
Subject: Double unquote in a macro
Date: 
Message-ID: <pan.2002.10.15.05.32.22.382402@consulting.net.nz>
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

From: Timothy Moore
Subject: Re: Double unquote in a macro
Date: 
Message-ID: <aogb4j$uot$0@216.39.145.192>
"Adam Warner" <······@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
From: Adam Warner
Subject: Re: Double unquote in a macro
Date: 
Message-ID: <pan.2002.10.15.06.36.05.725128@consulting.net.nz>
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
From: Martin Simmons
Subject: Re: Double unquote in a macro
Date: 
Message-ID: <3dac098c$0$1289$ed9e5944@reading.news.pipex.net>
"Adam Warner" <······@consulting.net.nz> wrote in message
···································@consulting.net.nz...
> 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!

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
······@xanalys.com
rot13 to reply
From: Adam Warner
Subject: Re: Double unquote in a macro
Date: 
Message-ID: <pan.2002.10.15.12.53.53.693197@consulting.net.nz>
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
From: Kenny Tilton
Subject: Re: Double unquote in a macro
Date: 
Message-ID: <RtOq9.7710$gB.2315653@twister.nyc.rr.com>
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
From: Adam Warner
Subject: Re: Double unquote in a macro
Date: 
Message-ID: <pan.2002.10.15.06.45.59.236956@consulting.net.nz>
Hi Kenny Tilton,

> 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.

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