From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Michael Heerdegen Newsgroups: gmane.emacs.help Subject: Re: problem with macro definitions Date: Sat, 29 Apr 2017 11:39:13 +0200 Message-ID: <878tmjr1ry.fsf@drachen> References: <20170427224407.GA5655@workstation> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1493458783 25557 195.159.176.226 (29 Apr 2017 09:39:43 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 29 Apr 2017 09:39:43 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Apr 29 11:39:38 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 1d4Oqn-0006Vb-OF for geh-help-gnu-emacs@m.gmane.org; Sat, 29 Apr 2017 11:39:37 +0200 Original-Received: from localhost ([::1]:40377 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d4Oqr-0008Mw-VF for geh-help-gnu-emacs@m.gmane.org; Sat, 29 Apr 2017 05:39:42 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:40657) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d4OqS-0008MS-9C for help-gnu-emacs@gnu.org; Sat, 29 Apr 2017 05:39:17 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d4OqP-0005sS-5z for help-gnu-emacs@gnu.org; Sat, 29 Apr 2017 05:39:16 -0400 Original-Received: from mout.web.de ([212.227.15.3]:60998) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d4OqO-0005rs-Rr for help-gnu-emacs@gnu.org; Sat, 29 Apr 2017 05:39:13 -0400 Original-Received: from drachen.dragon ([88.66.202.203]) by smtp.web.de (mrweb001 [213.165.67.108]) with ESMTPSA (Nemesis) id 0Lcgp5-1dmHpi2kCN-00kAvU; Sat, 29 Apr 2017 11:39:09 +0200 In-Reply-To: <20170427224407.GA5655@workstation> (hector's message of "Fri, 28 Apr 2017 00:44:07 +0200") X-Provags-ID: V03:K0:M5TS/57XfibGbZPsAk+nzXNtWLuRGx7O6MmAKoaenEgROGvbolJ QEil9RlKDx2uK1Gmak3k2g1o0dpvQuyBoHqlkOqkWAW9GuaQmal2qovRy5OSjX8EEdkUDNh Uvm3gehx1WvIqRsC7kCs+3XU996yGizMdP1s1XyxcAv61/EIeZLTCDnvfKjpnA68fkos7b4 jKUtpJnDIxbAEqZkB/8rQ== X-UI-Out-Filterresults: notjunk:1;V01:K0:h7Fu6HiZf6Y=:6GIpvHl43PqrE/RdKj1uAc 03uI9hQLpb03ouAFqa78YsQTL+6TeYGPiRH8GkTSv3aoW0USukz7pI9m8zWKgwnMZcv5Lqef0 rZzX826qWf1cwkk9Q9mmbfFaGudVj+eD8SZGDeYG9qjcjI1z9OmFko+xi3hKm2Tqken4Oxhv7 HPi11WFkWJgeS7CTckzz/rraKNRS2baPJ2cTC/4o+8X1YYfmm6GNchqvMT2zJ/mbR3jHfgvyI pOK5r6goyXXDrAcfmvTZjiHEfNunayF5Vk0Es8tenTGWSuyn79LcNLOarmbq50t/s89W1+M5G D2LPm1Rps0jnfn/qa36gHrSGuwEBomvhHjYv48VeUevAr3Wo0t14REoT/bZDgBDHQ5B2lz/IC dl/RUwS2903D1mA8CZ2R3Hoi1var6VTyPckMt/wqqTzwZPg2NbkOmb2hG+twdwkzaGuD0hFDQ kGWBikCzBQ9+yIFUwtuPrHm0Kehx3fuLUisds6QhkPoaWVj3BqjQm72outvbFoMT3qYA+Q7Q0 nEAx+EI5fb7KqGql99PEqrWqD8rLzLQckaaRaGwHrpD5PBUFm7TbOouCGpUwVdWMup3ysjOBB gj4Bh0OPKXjruedZOLFMDE78o/TP45ZQQayFryIDPufdkaiyPZ8MkfBCw62FYJMGm0k0TCV7f o8ZkjohwKqIJWN/q0QvZytsnJNnKImBizowpWKlh80IrZbGoP97UqyPZgPWawoU9pYJrB6qW5 SVryrQTXKmFE4M25txMxaMj/arNFdY7KDeB78vRYH3whE4lINYPE457CJMxF+PrQW7sO1YMn X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.227.15.3 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:112898 Archived-At: 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 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.