Hi, I've got another problem. The fact is that what I want to do with
defmacro's declarations is to be able to compute the next type of
declaration:
(my-macro (1 2 3 4) (1 2 3 4) ... (5 6 7 2))
For example, to compute each list that conforms the arg-list.
But I don't know how to define the macro to get the list of args without
being evaluated.
One example I've tried to is:
(defmacro my-macro (&rest args)
`(car ,args)
)
And the result for the call:
(my-macro (1 2) (1 4) (3 1 4))
is that (1 2) is not a function name.
What do I do?
Thanx
> But I don't know how to define the macro to get the list of args
without
> being evaluated.
It is not defmacro problem. It takes all args quoted (without
evaluation). Probably you're trying to evaluate them later.
E.g.:
(defmacro xxx (&rest l) `(quote ,l))
(xxx (1 2) 3) -> ((1 2) 3)
> One example I've tried to is:
>
> (defmacro my-macro (&rest args)
> `(car ,args)
> )
>
> And the result for the call:
>
> (my-macro (1 2) (1 4) (3 1 4))
>
> is that (1 2) is not a function name.
>
> What do I do?
You're evaluating your args.
Macro expands to the:
(car ((1 2) (1 4) ...)) - this is where it fails, not in the macro
expansion
stage.
From: M Jared Finder
Subject: Re: Another problem with the defmacro
Date:
Message-ID: <421cac59$1@x-privat.org>
Asuka wrote:
> Hi, I've got another problem. The fact is that what I want to do with
> defmacro's declarations is to be able to compute the next type of
> declaration:
>
> (my-macro (1 2 3 4) (1 2 3 4) ... (5 6 7 2))
>
> For example, to compute each list that conforms the arg-list.
>
> But I don't know how to define the macro to get the list of args without
> being evaluated.
>
> One example I've tried to is:
>
> (defmacro my-macro (&rest args)
> `(car ,args)
> )
>
> And the result for the call:
>
> (my-macro (1 2) (1 4) (3 1 4))
>
> is that (1 2) is not a function name.
>
> What do I do?
It sounds like you're still having trouble understanding the concept
behind Lisp macros. Remember that a Lisp macro is a function used for
code generation -- it takes S-expressions as input and returns an
S-expression as its output. This is similar to plain old functions,
which take values as input and return a single value[1]. You can test a
macro with MACROEXPAND-1, which will expand all the macros in a form.
CL-USER> (defmacro my-macro (&rest args)
`(car ,args))
MY-MACRO
CL-USER> (macroexpand-1 '(my-macro (1 2) (1 4) (3 1 4)))
(CAR ((1 2) (1 4) (3 1 4)))
So typing (my-macro (1 2) (1 4) (3 1 4))) is the same as typing
(car ((1 2) (1 4) (3 1 4))). Do you see the problem?
While I'm not sure what you want MY-MACRO to do, I suspect that you want
one of these:
(defmacro my-macro (&rest args) (car args))
(defmacro my-macro (&rest args) `(car ',args))
-- MJF
[1] Ignoring multiple values, of course.
M Jared Finder <·····@hpalace.com> writes:
> Asuka wrote:
> > Hi, I've got another problem. The fact is that what I want to do with
> > defmacro's declarations is to be able to compute the next type of
> > declaration:
> > (my-macro (1 2 3 4) (1 2 3 4) ... (5 6 7 2))
> > For example, to compute each list that conforms the arg-list.
> > But I don't know how to define the macro to get the list of args
> > without
> > being evaluated.
> > One example I've tried to is:
> > (defmacro my-macro (&rest args)
> > `(car ,args)
> > )
> > And the result for the call:
> > (my-macro (1 2) (1 4) (3 1 4))
> > is that (1 2) is not a function name.
> > What do I do?
>
> It sounds like you're still having trouble understanding the concept
> behind Lisp macros. Remember that a Lisp macro is a function used for
> code generation -- it takes S-expressions as input and returns an
> S-expression as its output.
I second this. And you feel stuck, and are not yet comfortable
backquote, you can do this
(defun emit-code-for-my-macro (x y z)
...
...)
and then after thourougly testing this function (feel free to use
trace/break to see what happens), you just do
(defmacro my-macro (x y z)
(emit-code-for-my-macro x y z))
It's that easy. The syntax is a bit more arcane for a variable number of
arguments, so I'll just give it here.
(defun emit-code-for-my-macro (&rest args)
...
...)
(defmacro my-macro (&rest args)
(apply #'emit-code-for-my-macro args))
or in a more macrologic way
(defmacro my-macro (&rest args)
(cons 'emit-code-for-my-macro args))
I guess, yo can see what happens here.
--
If in doubt, argue.
vrotaru <······@centrum.cz> writes:
>
> (defmacro my-macro (&rest args)
> (apply #'emit-code-for-my-macro args))
>
> or in a more macrologic way
>
> (defmacro my-macro (&rest args)
> (cons 'emit-code-for-my-macro args))
>
> I guess, yo can see what happens here.
>
Well, the second variant will not work. Obviously.
Emit-code-for-my-macro will be called at run time, not compilation time.
Sometimes I'm typing faster then thinking.
--
If in doubt, test.
Asuka wrote:
> Hi, I've got another problem. The fact is that what I want to do with
> defmacro's declarations is to be able to compute the next type of
> declaration:
>
> (my-macro (1 2 3 4) (1 2 3 4) ... (5 6 7 2))
>
> For example, to compute each list that conforms the arg-list.
>
> But I don't know how to define the macro to get the list of args without
> being evaluated.
>
> One example I've tried to is:
>
> (defmacro my-macro (&rest args)
> `(car ,args)
> )
>
> And the result for the call:
>
> (my-macro (1 2) (1 4) (3 1 4))
>
> is that (1 2) is not a function name.
>
> What do I do?
Subtle fix:
(defmacro my-macro (&rest args)
`(car ',args))
kt
--
Cells? Cello? Cells-Gtk?: http://www.common-lisp.net/project/cells/
Why Lisp? http://lisp.tech.coop/RtL%20Highlight%20Film
"Doctor, I wrestled with reality for forty years, and I am happy to
state that I finally won out over it." -- Elwood P. Dowd
"Asuka" <········@hotmail.com> wrote in message
·················@nsnmpen3-gest.nuria.telefonica-data.net...
> Hi, I've got another problem. The fact is that what I want to do with
> defmacro's declarations is to be able to compute the next type of
> declaration:
>
> (my-macro (1 2 3 4) (1 2 3 4) ... (5 6 7 2))
>
> For example, to compute each list that conforms the arg-list.
>
> But I don't know how to define the macro to get the list of args without
> being evaluated.
>
> One example I've tried to is:
>
> (defmacro my-macro (&rest args)
> `(car ,args)
FWIW, I find it a good rule of thumb that if every one of your macros
arguments end up with a comma in front it you are probably writing a
function. Of course, not knowing what you really want to achieve it is hard
to tell you yours should be a function.
If the arguments above (the (1 2 3 4) things) are going to be hard-coded
then even if comma'd internally, a macro can provide a convenience by
removing the necessity of 'ing everything. But if those example arguments
would really be the result of some computation then you will have problems
trying to use my-macro with those.
--
Coby Beck
(remove #\Space "coby 101 @ big pond . com")