Subject: Re: unbound variable in macro
From: (Rob Warnock)
Date: 1999/10/04
Newsgroups: comp.lang.lisp
Message-ID: <7t9ajv$>
[My apologies in advance to Common Lisp purists for using certain
 non-Common-Lisp Scheme terminology in what follows. I understand
 that the terms "quasiquote", "unquote", and "unquote-splicing" are
 *not* part of Common Lisp, however I have found that they are often
 helpful in talking about the subforms of Common Lisps's "backquote"
 syntax. The HyperSpec even acknowledges this (I think) with an explicit
 reference to Scheme in " Notes about Backquote":

 Erik may still yell at me (sobeit), but IMHO this is one case where
 using Scheme terminology may be helpful to understanding Common Lisp.]

Coby <> wrote:
| (Rob Warnock) wrote:
| > Coby  <> wrote:
| > +---------------
| > | I tried:
| > | (defmacro trap-let-list (let-list global)
| > |    `(setf ,global (mapcar #, (mapcar #'car ,let-list))))
| > +---------------
| >
| > I don't know what you meant by that "#," in the first MAPCAR, but the
| I was meaning to apply the , as a function to each element of the list
| of car's

Uh... "," isn't a "function"!! It's shorthand *syntax* for [what Scheme calls]
an "unquote" operation (which isn't a function either -- it's also syntax).
Likewise, "`" and ",@" are shorthand syntax for the operations called [in
Scheme] "quasiquote" and "unquote-splicing", respectively. For example, my
suggested solution:

	(defmacro trap-let-list (let-list global)
	  `(setf ,global (list ,@(mapcar #'car let-list))))

is [in Scheme] the same as:

	(defmacro trap-let-list (let-list global)
	    (setf (unquote global)
		  (list (unquote-splicing (mapcar #'car let-list))))))

which, after expansion, [in either Scheme or CL] is exactly the same as:

	(defmacro trap-let-list (let-list global)
	  (list 'setf global (cons 'list (mapcar #'car let-list))))

[O.k., o.k., "exactly the same" only in the sense of "equal" to. ]

| I just couldn't seem to find the right combo of ,'s and @'s....

Suggestion: Whenever you get confused about that, hand-expand all the
quasiquotes, unquotes, and unquote-splicings according to the rules given
in the HyperSpec, Section 2-4-6, and see if what you have left makes sense.

In fact, I'll go even farther: Until you start feeling comfortable with
"defmacro", don't even *use* quasiquote, unquote, or unquote-splicing
at all! (At least for a little while...)  They're just conveniences
(albeit *extreme* conveniences!), and aren't actually necessary at all.


Rob Warnock, 8L-846
Applied Networking
Silicon Graphics, Inc.		Phone: 650-933-1673
1600 Amphitheatre Pkwy.		FAX: 650-933-0511
Mountain View, CA  94043	PP-ASEL-IA