Newbie doubt with macros ` , ,@
- From: Ashrentum <ashrentum@xxxxxxxxx>
- Date: Tue, 4 Mar 2008 08:13:13 -0800 (PST)
Hi. It is a newbie question about how parameters and vars are treated
inside macros and how to deal with the commas, back quotes, and commas-
at. I give this silly example:
(defmacro mymacro ()
(let ((name (gensym))
`(list ,(dotimes (,name 10 ,name) (print ,name))))) ;1-error
That is, I want, for whatever reasons, that the index variable for the
dotimes was the name generated by (gensym).
It is not possible to write commas inside a comma, so how can I get
inside of dotimes the name generated by (gensym)?
Of course the fix here would just be remove the comma before the
dotimes and it would be done. But that would generate a code with an
inner macro and later would expand it, therefore I wouldn't see the
prints. I want to make the first, i.e., run the the inner macro before
the outer macro and see the prints.
How can you call first an internal macro with parameters of the outer
macro?
My scenario is this:
I had to define in one supermacro many bindings that were exactly the
same, i.e. different variables but with the same value. So for this I
thought that could be nice define another macro that got the value in
one parameter and a list of variables to bind with:
(defmacro let-samevars (definition &rest vars)
(let ((defs))
(dolist (var vars)
(push (list var definition) defs))
`',(reverse defs)))
So a call like this: (let-samevars nil v1 v2 v3) would create: '((v1
nil) (v2 nil) (v3 nil)). Of course, this makes more sense if instead
of nil is a complex expression.
So, let's pretend the supermacro is something like:
(defmacro supermacro ()
(let* ((name (gensym)))
`(let ((,name 5)
,@(let-samevars name v1 v2)) ;2-error
(list ,name v1 v2))))
That is, I want to creat a let like (let's say (gensym) created #:g01)
(let* ((#:g01 5) (v1 #:g01) (v2 #:g01))
(list #:g01 v1 v2))
that hopefully would be (list 5 5 5)
But of course is incorrect because name in 2-error is 'name and not
#:g01. But here is mandatory to run first the inner macro. So how name
in the call to let-samevars can be #:g01 or the value generated by
(gensym)?
Is there a combination of commas that I don't know?
.
- Follow-Ups:
- Re: Newbie doubt with macros ` , ,@
- From: Thomas A. Russ
- Re: Newbie doubt with macros ` , ,@
- From: Ken Tilton
- Re: Newbie doubt with macros ` , ,@
- From: Pascal J. Bourguignon
- Re: Newbie doubt with macros ` , ,@
- Prev by Date: Re: Practical Lisp 2008
- Next by Date: sbcl exit hooks
- Previous by thread: Monitor remote Lisp image through SNMP
- Next by thread: Re: Newbie doubt with macros ` , ,@
- Index(es):
Relevant Pages
|