From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Oliver Scholz Newsgroups: gmane.emacs.help Subject: Re: 'let' for functions ? Date: Sat, 26 Apr 2003 22:01:51 +0200 Sender: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: <1036073066.48586.0@iris.uk.clara.net> <87vg3i4rq2.fsf@lynx.ionific.com> <87ptn912ya.fsf@cs.uga.edu> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: main.gmane.org 1051387653 29872 80.91.224.249 (26 Apr 2003 20:07:33 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Sat, 26 Apr 2003 20:07:33 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Sat Apr 26 22:07:31 2003 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 199VxK-0007lP-00 for ; Sat, 26 Apr 2003 22:07:31 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 199Vw7-0008R3-06 for gnu-help-gnu-emacs@m.gmane.org; Sat, 26 Apr 2003 16:06:15 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!logbridge.uoregon.edu!fu-berlin.de!uni-berlin.de!dialin-145-254-195-048.arcor-ip.NET!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 50 Original-NNTP-Posting-Host: dialin-145-254-195-048.arcor-ip.net (145.254.195.48) Original-X-Trace: fu-berlin.de 1051387508 9456377 145.254.195.48 (16 [87814]) X-Attribution: os X-Face: "HgH2sgK|bfH$; PiOJI6|qUCf.ve<51_Od(%ynHr?=>znn#~#oS>",F%B8&\vus),2AsPYb -n>PgddtGEn}s7kH?7kH{P_~vu?]OvVN^qD(L)>G^gDCl(U9n{:d>'DkilN!_K"eNzjrtI4Ya6;Td% IZGMbJ{lawG+'J>QXPZD&TwWU@^~A}f^zAb[Ru;CT(UA]c& User-Agent: Gnus/5.090019 (Oort Gnus v0.19) Emacs/21.3.50 (windows-nt) Cancel-Lock: sha1:mWdfJBDBi5rlCWM3VM8IrdVUGnY= Original-Xref: shelby.stanford.edu gnu.emacs.help:112383 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:8882 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:8882 Ed L Cashin writes: [...] > As an exercise I tried in vain to create a simple implementation of > labels that wouldn't require loading cl. I couldn't do it, though, > and had to get on with what I was supposed to be doing. ;) You need not fear the cl package. If you put `(eval-when-compile (require 'cl)' into your code, the compiled package won't require cl. However, since this is, as you say, a nice exercise, I give it a shot. (Although I am supposed to be doing something different, too ...) > I'd be very interested, though, to see such a simple implementation of > labels if there's an elisp guru who would enjoy trying to find it. > The hard part for me was recursion support. I am not a Lisp-Guru, but here is my version anyways. I'd like to hear whether there is any problem with this approach: (defmacro my-simple-labels (spec &rest body) ;; We `fset' the symbols temporarily and restore the original state ;; after the body form was executed. (let ((symbol (make-symbol "symbol")) (definition (make-symbol "definition")) (sfunc (make-symbol "sfunc"))) `(progn (let (,symbol) (dolist (,definition ',spec) (setq ,symbol (car ,definition)) ;; Store function definition, if necessary. (when (fboundp ,symbol) (put ,symbol 'my-simple-labels (symbol-function ,symbol))) ;; New function definition. (fset ,symbol (cons 'lambda (cdr ,definition))))) (unwind-protect (progn ,@body) ;; Clean-up (let (,sfunc) (dolist (,symbol ',(mapcar 'car spec)) (setq ,sfunc (get ,symbol 'my-simple-labels)) (if ,sfunc (fset ,symbol ,sfunc) (fmakunbound ,symbol)))))))) Oliver -- 7 Floréal an 211 de la Révolution Liberté, Egalité, Fraternité!