Re: ONCE-ONLY
- From: "Vladimir Zolotykh" <gsmith@xxxxxxxxxxxxx>
- Date: Sat, 30 Apr 2005 14:14:08 +0300
On Fri, 29 Apr 2005 17:27:35 +0200, Pascal Bourguignon <pjb@xxxxxxxxxxxxxxxxx> wrote:
[SKIP]
Unfortunately the use of anonymous gensyms doesn't help reading this. Let's correct it:
[246]> (defmacro once-only ((&rest names) &body body) (let ((gensyms (loop for n in names collect (gensym (string n))))) `(let (,@(loop for g in gensyms collect `(,g (gensym )))) `(let (,,@(loop for g in gensyms for n in names collect ``(,,g ,,n))) ,(let (,@(loop for n in names for g in gensyms collect `(,n ,g))) ,@body))))) ONCE-ONLY [41]> (macroexpand-1 '(once-only (a b) (list 'list a b))) (LET ((#:A4103 (GENSYM)) (#:B4104 (GENSYM))) (LIST 'LET (LIST (LIST #:A4103 A) (LIST #:B4104 B)) (LET ((A #:A4103) (B #:B4104)) (LIST 'LIST A B)))) ; T
Now we see that this macro expands to code that return a s-expression.
If you evaluate this s-expression you get what macros using once-only
This is probably the key of my misunderstanding. You say "If you evaluate this s-expression". Isn't this the job of the evaluator? This additional level of evaluation proved quite surprising for me. A macro produces output (a list), if this list's CAR isn't a macro the macroexpansion is done, no further processing is performed during macroexpansion time, right? ONCE-ONLY (from your example) expands to a list which doesn't contain macro name as its CAR(s). So at that moment macroexpansion stopped and so did I, unable to solve the puzzle. Where does this additional evaluation come from?
get. To do that, we remove "#:", and we add templates for the arguments A and B:
[47]> (let ((A :A-template) (b :b-template)) (LET ((A4103 (GENSYM)) (B4104 (GENSYM))) (LIST 'LET (LIST (LIST A4103 A) (LIST B4104 B)) (LET ((A A4103) (B B4104)) (LIST 'LIST A B)))))
(LET ((#:G4109 :A-TEMPLATE) (#:G4110 :B-TEMPLATE)) (LIST #:G4109 #:G4110))
[SKIP] -- Vladimir Zolotykh .
- Follow-Ups:
- Re: ONCE-ONLY
- From: Pascal Bourguignon
- Re: ONCE-ONLY
- References:
- ONCE-ONLY
- From: Vladimir Zolotykh
- Re: ONCE-ONLY
- From: Pascal Bourguignon
- ONCE-ONLY
- Prev by Date: Re: What next after 'Practical' ??
- Next by Date: Re: What next after 'Practical' ??
- Previous by thread: Re: ONCE-ONLY
- Next by thread: Re: ONCE-ONLY
- Index(es):
Relevant Pages
|