Hi,
consider the following:
CL-USER> (defmacro bar (x)
(print (type-of x))
`(type-of ,x))
BAR
CL-USER> (bar 'a)
CONS
SYMBOL
I don't understand why the type-of operator returns CONS while the
same operator with the same argument returns SYMBOL at the REPL or in
a defun.
-Nicolas
In article
<····································@x16g2000prn.googlegroups.com>,
Nicolas Edel <············@gmail.com> wrote:
> Hi,
>
> consider the following:
>
> CL-USER> (defmacro bar (x)
> (print (type-of x))
> `(type-of ,x))
> BAR
> CL-USER> (bar 'a)
>
> CONS
> SYMBOL
>
> I don't understand why the type-of operator returns CONS while the
> same operator with the same argument returns SYMBOL at the REPL or in
> a defun.
>
> -Nicolas
(bar 'a) = (bar (quote a))
Now the macro executes similar to this:
(let ((x '(quote a)))
(type-of x))
-> CONS
(quote a) is the source and it is a CONS.
and expands (bar 'a) into (type-of 'a)
Executing: (type-of (quote a)) -> symbol
(quote a) will be evaluated into the symbol A
and this symbol will be passed to TYPE-OF.
TYPE-OF then thinks that the symbol A is a symbol.
Remember, during macro expansion the s-expression SOURCE is bound to
the macro parameters. Not values. SOURCE. The
macro then generates new source. This new source will
be executed.
--
http://lispm.dyndns.org/
Rainer Joswig wrote:
> In article
> <····································@x16g2000prn.googlegroups.com>,
> Nicolas Edel <············@gmail.com> wrote:
>
> > Hi,
> >
> > consider the following:
> >
> > CL-USER> (defmacro bar (x)
> > (print (type-of x))
> > `(type-of ,x))
> > BAR
> > CL-USER> (bar 'a)
> >
> > CONS
> > SYMBOL
> >
> > I don't understand why the type-of operator returns CONS while the
> > same operator with the same argument returns SYMBOL at the REPL or in
> > a defun.
> >
> > -Nicolas
>
> (bar 'a) = (bar (quote a))
>
> Now the macro executes similar to this:
>
> (let ((x '(quote a)))
> (type-of x))
>
> -> CONS
>
> (quote a) is the source and it is a CONS.
>
> and expands (bar 'a) into (type-of 'a)
>
>
> Executing: (type-of (quote a)) -> symbol
>
> (quote a) will be evaluated into the symbol A
> and this symbol will be passed to TYPE-OF.
> TYPE-OF then thinks that the symbol A is a symbol.
>
> Remember, during macro expansion the s-expression SOURCE is bound to
> the macro parameters. Not values. SOURCE. The
> macro then generates new source. This new source will
> be executed.
>
Crystal clear. Thanks.
-Nicolas
Nicolas Edel wrote:
> Hi,
>
> consider the following:
>
> CL-USER> (defmacro bar (x)
> (print (type-of x))
> `(type-of ,x))
> BAR
> CL-USER> (bar 'a)
>
> CONS
> SYMBOL
>
> I don't understand why the type-of operator returns CONS while the
> same operator with the same argument returns SYMBOL at the REPL or in
> a defun.
You were close, so very very close. All you had to do was:
(print (list (type-of x) x))
Quite a rush I recall when I got launched off this bit of the learning
curve, one of those instant enlightenment Zen deals.
kt