Suppose I wanted to create a sort of "anonymous" (one-shot) macro. For
instance, a back-quoted list (I know a back-quoted list is not a
macro, but just to get my point across) whose code is to be expanded
at compile-time, but without necessarily creating a full-blown, named
macro. In short, something like LAMBDA but for macros instead of
functions. Is this possible?
I realize I could pass the list to EVAL, but I don't want to do that
because the evaluation occurs during runtime (and for other reasons
regarding lexical scoping).
On Apr 2, 11:24 am, ···········@gmail.com wrote:
> Suppose I wanted to create a sort of "anonymous" (one-shot) macro. For
> instance, a back-quoted list (I know a back-quoted list is not a
> macro, but just to get my point across) whose code is to be expanded
> at compile-time, but without necessarily creating a full-blown, named
> macro. In short, something like LAMBDA but for macros instead of
> functions. Is this possible?
Would MACROLET be a good compromise?
More discussion here:
http://groups.google.com/group/comp.lang.lisp/msg/4da9387c1b81573e?dmode=source
Tayssir
On Apr 2, 10:36 am, Tayssir John Gabbour <············@googlemail.com>
wrote:
>
> Would MACROLET be a good compromise?
I was going to ask if
(macrolet ((x ...))
(x ...))
Does the right thing by toplevlness and so on, but I think the
hyperspec says it does, so this would be a good answer.
···········@gmail.com wrote:
> Suppose I wanted to create a sort of "anonymous" (one-shot) macro. For
> instance, a back-quoted list (I know a back-quoted list is not a
> macro, but just to get my point across) whose code is to be expanded
> at compile-time, but without necessarily creating a full-blown, named
> macro. In short, something like LAMBDA but for macros instead of
> functions. Is this possible?
If you want to use the macro once, why using a macro at all? Inside of
functions you could use macrolet to define a local macro, which you can use
multiple times.
--
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
On Apr 2, 11:33 am, Frank Buss <····@frank-buss.de> wrote:
> ···········@gmail.com wrote:
> > Suppose I wanted to create a sort of "anonymous" (one-shot) macro. For
> > instance, a back-quoted list (I know a back-quoted list is not a
> > macro, but just to get my point across) whose code is to be expanded
> > at compile-time, but without necessarily creating a full-blown, named
> > macro. In short, something like LAMBDA but for macros instead of
> > functions. Is this possible?
>
> If you want to use the macro once, why using a macro at all? Inside of
> functions you could use macrolet to define a local macro, which you can use
> multiple times.
Because sometimes it's easier and safer to write the code to write
some code, rather than writing the desired code directly. For
example, would you rather write out the definitions to the functions
c[ad]{2-4}r or write a macro that expands into their definitions?
Whoops, now you need to define the setf functions, too.
···········@gmail.com wrote:
> Suppose I wanted to create a sort of "anonymous" (one-shot) macro. For
> instance, a back-quoted list (I know a back-quoted list is not a
> macro, but just to get my point across) whose code is to be expanded
> at compile-time, but without necessarily creating a full-blown, named
> macro. In short, something like LAMBDA but for macros instead of
> functions. Is this possible?
>
> I realize I could pass the list to EVAL, but I don't want to do that
> because the evaluation occurs during runtime (and for other reasons
> regarding lexical scoping).
Try to implement it.
Here is a suggestion for the use of anonymous macros:
(amacro ((a b &body body) `(prog2 a b ,@body))
(print 1)
(print 2)
(print 3))
Here is the beginning of the macro definition:
(defmacro amacro ((lambda-list expression) &body body)
...)
(Don't make the mistake and try to define first-class macros. That
doesn't work.)
Pascal
--
1st European Lisp Symposium (ELS'08)
http://prog.vub.ac.be/~pcostanza/els08/
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
Pascal Costanza wrote:
> (Don't make the mistake and try to define first-class macros. That
> doesn't work.)
Sure it does. ;-) We just have to take your quote out of context.
;; "first-class" macros
(defmacro mapmacro (macro list)
(cond
((symbolp macro)
(cons 'progn
(loop for l in list
collect (list macro l))))
((listp macro)
(let ((m (gensym)))
(append
`(macrolet (,(cons m macro)))
(loop for l in list
collect (list m l)))))))
(defmacro m1 (x)
`(+ ,x 3))
(mapmacro m1 (1 2 3))
(mapmacro ((x) (+ x 3)) (1 2 3))
;; or
(defmacro mapmacro (macro list)
(cond
((symbolp macro)
(cons 'progn
(loop for l in list
collect (list macro l))))
((and (listp macro)
(equal (car macro) 'mambda))
(let ((m (gensym)))
(append
`(macrolet (,(cons m (cdr macro))))
(loop for l in list
collect (list m l)))))))
(mapmacro (mambda (x) (+ x 3)) (1 2 3))
- Daniel
rr> Suppose I wanted to create a sort of "anonymous" (one-shot) macro. For
rr> instance, a back-quoted list (I know a back-quoted list is not a
rr> macro, but just to get my point across) whose code is to be expanded
rr> at compile-time,
you can expand code at read-time via "#.". that should be fine for most
purposes