From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: David Kastrup Newsgroups: gmane.emacs.help Subject: Re: defining many similar functions using macros Date: Sun, 03 Oct 2004 10:18:36 +0200 Organization: Organization?!? Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1096791621 1050 80.91.229.6 (3 Oct 2004 08:20:21 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sun, 3 Oct 2004 08:20:21 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Oct 03 10:20:10 2004 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1CE1bG-0008FP-00 for ; Sun, 03 Oct 2004 10:20:10 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1CE1hm-00064C-SS for geh-help-gnu-emacs@m.gmane.org; Sun, 03 Oct 2004 04:26:55 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!fu-berlin.de!uni-berlin.de!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 65 Original-X-Trace: news.uni-berlin.de xx22C/eXE1SxWfZuGrETqAzohwl9QiA87fVrC1eAQPDJE4646R X-Face: 2FEFf>]>q>2iw=B6, xrUubRI>pR&Ml9=ao@P@i)L:\urd*t9M~y1^:+Y]'C0~{mAl`oQuAl \!3KEIp?*w`|bL5qr,H)LFO6Q=qx~iH4DN; i"; /yuIsqbLLCh/!U#X[S~(5eZ41to5f%E@'ELIi$t^ Vc\LWP@J5p^rst0+('>Er0=^1{]M9!p?&:\z]|;&=NP3AhB!B_bi^]Pfkw User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3.50 (gnu/linux) Cancel-Lock: sha1:78zo3weve16PtNQF6gIstwSzqzA= Original-Xref: shelby.stanford.edu gnu.emacs.help:125665 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 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 Xref: main.gmane.org gmane.emacs.help:21024 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:21024 Joe Corneli writes: > I have a lot of functions that are very similar: > > (defun tex-alpha () > (interactive) > (insert "\\alpha")) > > (defun tex-beta () > (interactive) > (insert "\\beta")) > > ... > > I would like to define them all in one go: > > (dolist (elt '("alpha" > "beta" > ...)) > (define-tex-symbol elt)) > > This seems like a good chance to use a macro. Not really. > My first experiment along these lines fails however, and I could use > some help re-designing it. > > This macro works on single elements: > > (defmacro define-tex-symbol (name) > `(defun ,(intern (concat "tex-" name)) () > (interactive) > (insert "\\" ,name))) > > E.g. (define-tex-symbol "alpha") ;=> tex-alpha > > But > > (dolist (elt '("alpha" > "beta")) > (define-tex-symbol elt)) > > triggers an error: > > Debugger entered--Lisp error: (wrong-type-argument sequencep elt) > concat("tex-" elt) > (intern (concat "tex-" name)) > (list (quote defun) (intern (concat "tex-" name)) nil (quote (interactive)) (list (quote insert) "\\" name)) > ... > > There seem to be some subtleties associated with macro expansion > that I'm missing here. Nothing subtle here at all. macros are substitutions at compile time (that's why they are efficient). So the "intern" gets evaluated at compile time. At that time, `name' is bound to the unevaled symbol `elt'. So what defun would you expect to result from your code? Really. Take pencil and paper and write down the expansion you wanted to occur. You can't calculate the symbol to define at compile time, because it is supposed to be a different one each time through the loop. -- David Kastrup, Kriemhildstr. 15, 44793 Bochum