unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: hector <hectorlahoz@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: problem with macro definitions
Date: Tue, 9 May 2017 10:14:35 +0200	[thread overview]
Message-ID: <20170509081435.GA3514@workstation> (raw)
In-Reply-To: <20170429160632.GA3291@workstation>

On Sat, Apr 29, 2017 at 06:06:32PM +0200, hector wrote:
> Thank you for your reply.
> 
> On Sat, Apr 29, 2017 at 11:39:13AM +0200, Michael Heerdegen wrote:
> > 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 know. In this case when the list is constant it seemed to be right.
> 
> > > 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.
> 
> Let me rephrase your statement:
> 
> "You gain nothing when you write this as a macro.  In this
> implementation, the index is calculated at run-time"
> 
> I'm aware of that now. This is not what I meant.
> I'll try to rewrite it.

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.
I come from C. I know each language has its idioms.
I don't know if this is good LISP. But I was just trying
to get the equivalent of a C enum. In most cases it can
be achieved with symbols but in this case I needed the
integer. With the improvement that I don't traverse the
whole list, just until the element (needle) is found.

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



  reply	other threads:[~2017-05-09  8:14 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
2017-04-29  9:52   ` Michael Heerdegen
2017-04-29 16:06   ` hector
2017-05-09  8:14     ` hector [this message]
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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=20170509081435.GA3514@workstation \
    --to=hectorlahoz@gmail.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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).