all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Filipp Gunbin <fgunbin@fastmail.fm>
To: Xue Fuqiao <xfq.free@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: About `macroexpand'
Date: Sun, 30 Dec 2012 07:50:45 +0400	[thread overview]
Message-ID: <86vcbk8aga.fsf@w2139spb.ru.yotateam.com> (raw)
In-Reply-To: <20121230103931.f8b844934dbcf3742fd34e21@gmail.com> (Xue Fuqiao's message of "Sun, 30 Dec 2012 10:39:31 +0800")

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



  reply	other threads:[~2012-12-30  3:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-30  2:39 About `macroexpand' Xue Fuqiao
2012-12-30  3:50 ` Filipp Gunbin [this message]
2012-12-30  5:35   ` Xue Fuqiao
2012-12-30  7:06     ` Filipp Gunbin
2012-12-30  7:40       ` Xue Fuqiao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86vcbk8aga.fsf@w2139spb.ru.yotateam.com \
    --to=fgunbin@fastmail.fm \
    --cc=help-gnu-emacs@gnu.org \
    --cc=xfq.free@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.