all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Yuri Khan <yuri.v.khan@gmail.com>
To: Heime <heimeborgia@protonmail.com>
Cc: Heime via Users list for the GNU Emacs text editor
	<help-gnu-emacs@gnu.org>
Subject: Re: macros and macroexpand
Date: Mon, 7 Aug 2023 18:46:35 +0700	[thread overview]
Message-ID: <CAP_d_8W227XyWf0JihuabNrTaJaQoBaKNouTfBiWXeZFRSmPjg@mail.gmail.com> (raw)
In-Reply-To: <vK2GWkxpqGqPlc3-e1C8U9jFMzj_D3_fTnTeTrGfAa67tCiSJv-_LnOzg647JK0IkWzVnv7E7BRqAE5-Eb_cQ1wyZ_SityPYX6JjJwxHolY=@protonmail.com>

On Mon, 7 Aug 2023 at 18:04, Heime <heimeborgia@protonmail.com> wrote:

> I have made a macro and know that they are supposed to return
> expanded code for use.  Still I cannot understand the need to
> call "macroexpand".  Should't the macro already perform the
> expansion ?

You should be posting small examples of code that you’re trying,
otherwise, there is high chance people will either misunderstand you
or just disregard your questions as ill-posed.

----

When you define a macro, you indeed write the definition similarly to
a function that returns expanded code.

    (defmacro foo (&rest body)
      `(bar ,@body))

When you evaluate a form that references a macro, Elisp will (1)
expand the macro, and (2) evaluate the result of the expansion:

    (foo 'quux)
    ⇒ Debugger entered--Lisp error: (void-function bar)

On the other hand, calling ‘macroexpand’ on a data representation of
that form will just return the expansion result:

    (macroexpand '(foo 'quux))
    ⇒ (bar 'quux)

In this example, I did not bother to define ‘bar’, so Elisp assumes it
would be a function and complains at evaluation time. But I could
further define ‘bar’ as a macro:

    (defmacro bar (&rest body)
      `(baz ,@body))

In this case, evaluating the original form shows that Elisp expanded
both macros ‘foo’ and ‘bar’, and then tried to call the undefined
function ‘baz’:

    (foo 'quux)
    ⇒ Debugger entered--Lisp error: (void-function baz)

Meanwhile, ‘macroexpand’ still just expands a single level of macros:

    (macroexpand '(foo 'quux))
    ⇒ (bar 'quux)

and you can invoke it repeatedly until you get to the fixed point:

    (macroexpand (macroexpand '(foo 'quux)))
    ⇒ (baz 'quux)

    (macroexpand (macroexpand (macroexpand '(foo 'quux))))
    ⇒ (baz 'quux)



  reply	other threads:[~2023-08-07 11:46 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-07 11:03 macros and macroexpand Heime
2023-08-07 11:46 ` Yuri Khan [this message]
2023-08-07 12:43   ` Heime
2023-08-07 14:22     ` Philip Kaludercic
2023-08-07 18:06       ` Heime
2023-08-07 20:08         ` Philip Kaludercic
2023-08-07 20:56           ` Heime
2023-08-07 22:10             ` Emanuel Berg
2023-08-08 20:35               ` Heime
2023-08-08  5:57             ` Philip Kaludercic
2023-08-07 14:28 ` [External] : " Drew Adams
2023-08-07 18:22   ` Heime
2023-08-07 19:04   ` Emanuel Berg
2023-08-07 18:59 ` Emanuel Berg

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=CAP_d_8W227XyWf0JihuabNrTaJaQoBaKNouTfBiWXeZFRSmPjg@mail.gmail.com \
    --to=yuri.v.khan@gmail.com \
    --cc=heimeborgia@protonmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /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.