From: Michael Heerdegen <michael_heerdegen@web.de>
To: help-gnu-emacs@gnu.org
Subject: Re: problem with macro definitions
Date: Sat, 29 Apr 2017 11:39:13 +0200 [thread overview]
Message-ID: <878tmjr1ry.fsf@drachen> (raw)
In-Reply-To: <20170427224407.GA5655@workstation> (hector's message of "Fri, 28 Apr 2017 00:44:07 +0200")
hector <hectorlahoz@gmail.com> writes:
> I was looking for the complementary of nth, that is, a function that
> returns the index of an element. Since it can be done at compilation
> time
Not if you want to use it on values that are known only at run-time.
> I thought it was a good candidate for a macro:
No, not really:
> (defmacro idx (list telt)
> `(let (found
> (idx 0))
> (dolist (elt ,list found)
> (when (eq elt ,telt)
> (setq found idx))
> (setq idx (1+ idx)))))
You loose nothing when you rewrite this as a function. In this
implementation, the index is calculated at run-time.
> Then I wrote another one:
>
> (defconst start-states '(initial red blue yellow))
> (defconst shift-val 10)
>
> (defmacro state-eof (st)
> (let ((val (+ shift-val (idx start-states st))))
> val))
That comes closer to what you intend to implement.
> Probably there is a better way to accomplish this. Anyway I'd like to
> know why it doesn't work.
>
> This yields 2 as expected:
> (idx start-states 'blue)
>
> so I expected this to return 12:
> (state-eof 'blue)
Note that macros don't evaluate their arguments. The argument 'blue you
pass, a read syntax for (quote blue), is a list of two elements. This
doesn't appear as an element of `start-states' which consists of four
symbols.
Your macro `state-eof' actually does find the value at compile time.
It's still an unusual use case for `defmacro' which is normally code
transformation. Your `state-eof' calls just expand to a number. You
can do this, but in real life one would probably prefer a different
solution, like defining this number as a `defconst', depending on the
actual use case.
Regards,
Michael.
next prev parent reply other threads:[~2017-04-29 9:39 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-27 22:44 problem with macro definitions hector
2017-04-29 9:39 ` Michael Heerdegen [this message]
2017-04-29 9:52 ` Michael Heerdegen
2017-04-29 16:06 ` hector
2017-05-09 8:14 ` hector
2017-05-09 14:16 ` Michael Heerdegen
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=878tmjr1ry.fsf@drachen \
--to=michael_heerdegen@web.de \
--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.