Hi,
I'm trying to understand a little more about how Lisp macros work, in
particular how to use macros to define other macros or functions. I
have a copy of PAIP, but I'm currently taking the author's advice and
working through a more introductory book on Lisp first. The book I
have is _A Programmer's Guide to Common Lisp_ by Deborah G. Tatar.
One of the code snippets from the book is:
(defmacro make-macro-named (name)
`(defmacro ,name ()
`(print ',name)))
According to the book, calling:
(make-macro-named fish)
should expand to:
(defmacro fish () `(print 'fish))
When I try and evaluate (fish), then I get:
9. Break [19]> ;;; Evaluating fish
*** - EVAL: variable NAME has no value
Likewise, trying (macroexpand '(fish)) gets me:
10. Break [20]> ;;; Evaluating macroexpand
*** - EVAL: variable NAME has no value
I'm obviously missing something important and I apologize if this is a
FAQ, but I've search Google and the web and have not been able to find
any examples. Does anyone have a small snippet of code that they
would be willing to share showing how to define macros or functions on
the fly?
Thank you very much.
······@hotmail.com (Alex LaFontaine) writes:
> _A Programmer's Guide to Common Lisp_ by Deborah G. Tatar.
>
> (defmacro make-macro-named (name)
> `(defmacro ,name ()
> `(print ',name)))
Hi,
I'm also still quiet new to CL, so others might be better suited to
elaborate on this.
However, I think there might be a typo in the book. I tried the
following (in CMUCL) and it worked:
* (defmacro make-macro-named (name)
`(defmacro ,name ()
`(print ',',name)))
MAKE-MACRO-NAMED
* (macroexpand-1 '(make-macro-named fish))
(DEFMACRO FISH () '(PRINT 'FISH))
T
* (make-macro-named fish)
FISH
* (fish)
FISH
FISH
Good luck,
Edi.
PS: Paul Graham's "On Lisp" has a lot of interesting stuff about
macros (and macro-defining macros).
In article <····························@posting.google.com>,
Alex LaFontaine wrote:
> I'm trying to understand a little more about how Lisp macros work, in
> particular how to use macros to define other macros or functions. I
> have a copy of PAIP, but I'm currently taking the author's advice and
> working through a more introductory book on Lisp first. The book I
> have is _A Programmer's Guide to Common Lisp_ by Deborah G. Tatar.
>
> One of the code snippets from the book is:
>
> (defmacro make-macro-named (name)
> `(defmacro ,name ()
> `(print ',name)))
I've never written a macro-defining macro by myself, but
I think the second ` of the above should be ' :
(defmacro make-macro-named (name)
`(defmacro ,name ()
'(print ',name)))
There's a section "Backquote Notation" in PAIP page 67-68.
regards,
abe
--
keke at mac com
--
<keke at mac com>
In article <····························@posting.google.com>, Alex
LaFontaine wrote:
>When I try and evaluate (fish), then I get:
>
>9. Break [19]> ;;; Evaluating fish
>*** - EVAL: variable NAME has no value
>
>Likewise, trying (macroexpand '(fish)) gets me:
Instead, try doing macroexpand on '(make-macro-named fish) to see what
mistake it made in generating the form that defined the fish macro.
Alex LaFontaine wrote:
> I'm trying to understand a little more about how Lisp macros work, in
> particular how to use macros to define other macros or functions. I
> have a copy of PAIP, but I'm currently taking the author's advice and
> working through a more introductory book on Lisp first. The book I
> have is _A Programmer's Guide to Common Lisp_ by Deborah G. Tatar.
I happen to read the same book at the moment. Of the Lisp tutorials I
found in our library, I liked it best so far. Funny that all those books
are at least 14 years old.
I don't know if that helps you, but here the code worked:
,---------[ clisp-session ]
| [1]> (defmacro make-macro-named (name)
| `(defmacro ,name ()
| '(print ',name)))
| MAKE-MACRO-NAMED
| [2]> (make-macro-named fish)
| FISH
| [3]> (fish)
|
| FISH
| FISH
| [4]>
`---------
In fact, I guess it doesn't help. Mostly, I wanted to say you're not
alone in learning Lisp with this book.
--
Wolfgang Mederle
················@stud.uni-muenchen.de
http://www.mederle.de/
ICQ# 1435333
In article <·············@DS9.mederle.de>, Wolfgang Mederle wrote:
>Alex LaFontaine wrote:
>
>> I'm trying to understand a little more about how Lisp macros work, in
>> particular how to use macros to define other macros or functions. I
>> have a copy of PAIP, but I'm currently taking the author's advice and
>> working through a more introductory book on Lisp first. The book I
>> have is _A Programmer's Guide to Common Lisp_ by Deborah G. Tatar.
>
>I happen to read the same book at the moment. Of the Lisp tutorials I
>found in our library, I liked it best so far. Funny that all those books
>are at least 14 years old.
>
>I don't know if that helps you, but here the code worked:
>
>,---------[ clisp-session ]
>| [1]> (defmacro make-macro-named (name)
>| `(defmacro ,name ()
>| '(print ',name)))
Note that here you have a regular quote around the (print ...).
But what Alex LaFontaine posted has a backquote there.
The idea is that (defmacro ,name () '(print ',name)) is a template,
in which we insert the value of ,name in both places. So that the call
(mace-macro-named foo) will result in the form
(defmacro foo () '(print 'foo)) being returned. There is no surviving
backquote which appears in macro foo; that macro simply returns
the fixed list (print 'foo).
This is either an error in the book that you corrected in your
transcription above, or a transcription error on Alex's part.
Kaz Kylheku wrote:
> This is either an error in the book that you corrected in your
> transcription above, or a transcription error on Alex's part.
Must be the latter (or a "bugfix" in my issue of the book), as I typed it
in directly from the book.
--
Wolfgang Mederle
················@stud.uni-muenchen.de
http://www.mederle.de/
ICQ# 1435333
Wow, thanks so much to everybody -- this thread gave me two different
ways to solve my problem -- plus cleared up a problem that I was
having using macroexpand correctly.
I have to say that reading this newsgroup everyday is the best
possible Lisp tutorial. I see that there is already another thread on
the loop macro, which is another amazing feature of CL that I have yet
to master.
Oh, just to clear up one thing -- I double checked the Tatar book and
it definitely looks like there was a typo that must have cleared up in
a later edition. I really like the book too -- but trying to learn
backquote notation is tricky enough without confusing quotes and
backquotes.
Thanks again to everyone, I'll be going back to lurking and reading
more for a while. . .