From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: hector Newsgroups: gmane.emacs.help Subject: Re: problem with macro definitions Date: Tue, 9 May 2017 10:14:35 +0200 Message-ID: <20170509081435.GA3514@workstation> References: <20170427224407.GA5655@workstation> <878tmjr1ry.fsf@drachen> <20170429160632.GA3291@workstation> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1494318954 32111 195.159.176.226 (9 May 2017 08:35:54 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 9 May 2017 08:35:54 +0000 (UTC) User-Agent: Mutt/1.5.20 (2009-06-14) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue May 09 10:35:47 2017 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d80cV-0008Am-36 for geh-help-gnu-emacs@m.gmane.org; Tue, 09 May 2017 10:35:47 +0200 Original-Received: from localhost ([::1]:35790 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d80ca-0000iL-Lw for geh-help-gnu-emacs@m.gmane.org; Tue, 09 May 2017 04:35:52 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:46422) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d80I8-0007QS-U6 for help-gnu-emacs@gnu.org; Tue, 09 May 2017 04:14:45 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d80I3-0003OI-Vu for help-gnu-emacs@gnu.org; Tue, 09 May 2017 04:14:44 -0400 Original-Received: from mail-wr0-x234.google.com ([2a00:1450:400c:c0c::234]:34248) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d80I3-0003O1-Ol for help-gnu-emacs@gnu.org; Tue, 09 May 2017 04:14:39 -0400 Original-Received: by mail-wr0-x234.google.com with SMTP id l9so62964827wre.1 for ; Tue, 09 May 2017 01:14:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:date:to:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to:user-agent; bh=6jMQ5GTD6lPEaP/Xp598AMl6kArfF6IxpxQ78c4Nx9c=; b=a1QnWtFLA1ZPIwE87ZOX8RlbqI0eJYGg7hd5VOynLa+jU4yn2/VwuNWJpA852KegRi kxnylUr6Rq1D34DDH3qQgrwbvY8L0rJeYFGUDGCxkK3nBBgAcFiqX/FbH8OmXsU5hIaI 6E0n+EydkVP3Oq2Ksy/yfD4ENdeDSX04B3WOj781fs4ce9v1gdt8B014mYxQCDzpvVT3 8m4Aa9GJ41H61Y52ZmLK5IwugCWVy1gQvvCPAcdZEYWAYtMY0t+lFm7pYWiLiH4sp2Vk HcbKvzAYBftjPGFWVP44tUDduo75jald5zBOg9C0Jc1rqN34hOkhdJTbxGSxDC94dmkQ sf0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:subject:message-id:mail-followup-to :references:mime-version:content-disposition:in-reply-to:user-agent; bh=6jMQ5GTD6lPEaP/Xp598AMl6kArfF6IxpxQ78c4Nx9c=; b=F/1mk6ubqI6+Dwe66gBg1Ouf8rH5qZAYPXu42AZVDw1Nke14wAbccrAigA2MxgyzLj Ki3yy7ioCVlqDXttgfteB8mVGDLKsfwPJZ9LRl0HE/PRXChQep8cxuHUl65wYzsdpcof 96sK4xoquDk/bU8QUsGkn2HWjiOM43V4NYfrqqQOMCOQuQnxx+i9bggfX/YLplOnvqEN t1MyHCXwq7VNfJ5S4SmFhanQ3rXnRKdILzH4cz0sH4tipRi9WYmK+BJ2HNBYj/S0EqbA mY60JYt0NtZzRlMSctGbG+C38YvKyNPkPYanMd2u2OGrAEv4WXEf1yhOnuOkvLzWXBCF 9uFg== X-Gm-Message-State: AN3rC/43sieAZViYtg3LwktRpMLEYS+y16EzB14KUZ+tlvnqwUeDWJ8L D0JZZHH4M8qftnl9zFo= X-Received: by 10.223.150.18 with SMTP id b18mr42370659wra.98.1494317678437; Tue, 09 May 2017 01:14:38 -0700 (PDT) Original-Received: from workstation ([148.56.209.77]) by smtp.gmail.com with ESMTPSA id y63sm169866wme.31.2017.05.09.01.14.37 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 09 May 2017 01:14:37 -0700 (PDT) X-Google-Original-From: hector Mail-Followup-To: help-gnu-emacs@gnu.org Content-Disposition: inline In-Reply-To: <20170429160632.GA3291@workstation> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::234 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:112974 Archived-At: 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 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.