Newbie doubt with macros ` , ,@



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



Relevant Pages

  • Re: Newbie doubt with macros ` , ,@
    ... I realize you may have distilled this from a real macro attempt, but the problem is back there in the real macro, not with "commas inside commas". ... (defmacro let-samevars (definition &rest vars) ... nil) ). ...
    (comp.lang.lisp)
  • Re: defmacro
    ... (defmacro doloop (vars &body body) ... To write a macro, ...
    (comp.lang.lisp)
  • 2 macros and passing gensyms around
    ... when (listp var-and-column-name) ... (defmacro bind-vars ((&rest vars) ... data &body body) ... The name would be created in the with-bindable-columns macro and used in the ...
    (comp.lang.lisp)
  • Re: Newbie doubt with macros ` , ,@
    ... It is not possible to write commas inside a comma, ... macro and what should be a function. ... (defmacro let-samevars (definition &rest vars) ...
    (comp.lang.lisp)
  • Re: Why is this macro misbehaving?
    ... (defmacro with-url ((&rest vars) ... for subseq in subseqs collect ... your macro gets the list (REQUEST-UNHANDLED-PART ... REQUEST) as the SEQ argument. ...
    (comp.lang.lisp)