I am having trouble understanding parts of the chapter on Macros in CLtL2.
In particular, I am getting more and more confused about using &rest and
&body arguments.
I guess it is difficult for me to formulate a precise questions as to where
I am confused, except that I cannot understand the explanation there.
In particular, suppose one wants to write a macro "fred" which will take an
arbitrary list of arguments and does something with them.
The naive approach of "macroizing" a function definition will not work:
(defmacro fred (&rest)
`(do-something-with ,rest))
since when it tries to find the value for rest in the backquote, rest looks
like a function and lisp will try to treat it as a function call.
How would the correct way be to write such a macro?
Any hints are appreciated.
Thanks, Thomas.
--
Note: The address below is valid until Jan 15, 1993.
+--------------------------------+---------------------------------+
| Thomas Weigert | |
| Machine Inference Section | |
| Electrotechnical Laboratory | |
| Umezono 1-1-4, Tukuba-shi | ·······@{mcs.anl.gov,etl.go.jp} |
| Ibaraki 305, Japan | +81-298-58-5918 (phone+fax) |
+--------------------------------+---------------------------------+
In article <·····················@etlhit.etl.go.jp> ·······@etlhit.etl.go.jp (Thomas Weigert) writes:
I am having trouble understanding parts of the chapter on Macros in CLtL2.
In particular, I am getting more and more confused about using &rest and
&body arguments.
...
In particular, suppose one wants to write a macro "fred" which will take an
arbitrary list of arguments and does something with them.
The naive approach of "macroizing" a function definition will not work:
(defmacro fred (&rest)
`(do-something-with ,rest))
since when it tries to find the value for rest in the backquote, rest looks
like a function and lisp will try to treat it as a function call.
How would the correct way be to write such a macro?
Use the backquote splicing operator ",@". An example of its use:
(let ((foo '(b c d)))
`(a ,@foo e))
(A B C D E)
So your naive macro would be written as:
(defmacro fred (&rest rest)
`(do-something-with ,@rest))
It's true that the Macros chapter doesn't show a single use of ,@.
Check out the definition of backquote in the Input/Output chapter and
Appendix C.
--
Tim Moore ·····@cs.utah.edu {bellcore,hplabs}!utah-cs!moore
"Wind in my hair - Shifting and drifting - Mechanical music - Adrenaline surge"
- Rush
In article <·····················@etlhit.etl.go.jp> Thomas Weigert,
·······@etlhit.etl.go.jp writes:
>(defmacro fred (&rest)
> `(do-something-with ,rest))
How about:
(defmacro fred (&rest)
`(do-something-with ,@rest))
Tim Larkin
Federal Nutrition Laboratory
Tower Road
Ithaca, New York
····@cornell.edu
607-255-7008