Macro-Defining Macros 
Author Message
 Macro-Defining Macros

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.



Wed, 11 Feb 2004 22:10:30 GMT  
 Macro-Defining Macros

Quote:

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



Wed, 11 Feb 2004 22:27:19 GMT  
 Macro-Defining Macros

Quote:

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



Thu, 12 Feb 2004 01:03:39 GMT  
 Macro-Defining Macros

Quote:

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


Thu, 12 Feb 2004 03:40:19 GMT  
 Macro-Defining Macros

Quote:

> 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

http://www.mederle.de/
ICQ# 1435333



Thu, 12 Feb 2004 05:54:03 GMT  
 Macro-Defining Macros

Quote:


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



Thu, 12 Feb 2004 07:51:04 GMT  
 Macro-Defining Macros

Quote:

> 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

http://www.mederle.de/
ICQ# 1435333



Thu, 12 Feb 2004 08:37:17 GMT  
 Macro-Defining Macros
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. . .



Fri, 13 Feb 2004 09:57:18 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Macros defining macros with define-syntax

2. Question about a macro-defining macro

3. help sought for macro defining macro

4. Load-time problem with a macro that defines a macro

5. local-macro-defining macros (was Re: Better Dylan syntax?)

6. A macro involving two sub-macros - where the 2nd macro needs results from the first

7. define-macro -> define-syntax

8. define-macro vs define-syntax

9. Scheme macro source: define/keyed, for defining keyword-triggered arguments

10. macro -vs- macro/codeblock

11. Help with macros writing macros in Bigloo

12. syntax-rules macros with sub-macros

 

 
Powered by phpBB® Forum Software