[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: About `macroexpand'
From: |
Filipp Gunbin |
Subject: |
Re: About `macroexpand' |
Date: |
Sun, 30 Dec 2012 07:50:45 +0400 |
User-agent: |
Gnus/5.1299999999999999 (Gnus v5.13) Emacs/24.2 (cygwin) |
On 30/12/2012 06:39, Xue Fuqiao wrote:
> I wrote the following code:
> (defmacro t-becomes-nil (variable)
> (if (eq variable t)
> (setq variable nil)))
> (setq foo t)
> (macroexpand '(t-becomes-nil foo))
>
> The return value of `macroexpand' is nil. I don't know why. I think it
> should be:
> (if (eq foo t)
> (setq foo nil)))
>
> Can anybody help?
Inside your macro, the formal argument `variable' evaluates to `foo'
(that is, the actual argument, which _not yet evaluated_ itself).
Then it's value (`foo') is compared by `eq' with 't' and possibly
overwritten with `nil'. The comparison doesn't make any sense and what
is actually overwritten is the local value of `variable'.
A correct version is like that:
(defmacro t-becomes-nil (variable)
`(if (eq ,variable t)
(setq ,variable nil)))
This returns the list after the backquote as written except the
`,variable' is substituted for the local value of `variable', which is
`foo'. Then the calling program can evaluate the resulting form to
obtain final result.
More on this: (info "(elisp) Wrong Time").
--
Filipp Gunbin