help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: problem with macro definitions


From: Michael Heerdegen
Subject: Re: problem with macro definitions
Date: Tue, 09 May 2017 16:16:22 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux)

hector <hectorlahoz@gmail.com> writes:

> I did it. This is what I meant:
>
> (defmacro idx (list needle)
>   (let (found
>        (haystack (eval list))
>        (idx 0))
>     (while (not found)
>       (when (eq (car haystack) needle)
>         (setq found idx))
>       (setq idx (1+ idx))
>       (setq haystack (cdr haystack)))
>     found))
>
> The macro expansion is just a number.

Yes, that's true now.

> I'm not quite sure what the "(let ((haystack (eval list))))" does but
> it works.

A macro doesn't evaluate its arguments.  Normally, arguments are
evaluated when the expanded code (with argument expressions "pasted in")
is run.  You want to do the whole calculation at compile time, and you
want to allow LIST to be an expression (known at compile time), so you
need to evaluate it explicitly to get a list value.

The same applies to the NEEDLE argument btw.


Michael.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]