From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Ed L Cashin Newsgroups: gmane.emacs.help Subject: Re: 'let' for functions ? Date: Sat, 26 Apr 2003 16:20:28 -0400 Organization: negia.net Sender: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: <87he8l104z.fsf@cs.uga.edu> 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=us-ascii X-Trace: main.gmane.org 1051388816 812 80.91.224.249 (26 Apr 2003 20:26:56 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Sat, 26 Apr 2003 20:26:56 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Sat Apr 26 22:26:55 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 199WG7-0000Cy-00 for ; Sat, 26 Apr 2003 22:26:55 +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 199WEb-0001CV-07 for gnu-help-gnu-emacs@m.gmane.org; Sat, 26 Apr 2003 16:25:21 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-06!sn-post-02!sn-post-01!supernews.com!corp.supernews.com!not-for-mail Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.090014 (Oort Gnus v0.14) Emacs/21.2 (i386-debian-linux-gnu) Cancel-Lock: sha1:5FxEuRN/WbwEWMNYG4UeKLfs90Q= Cache-Post-Path: alpha.negia.net!unknown@ppp63-172-85-115.silverstreakdsl.com X-Cache: nntpcache 2.4.0b5 (see http://www.nntpcache.org/) Original-X-Complaints-To: abuse@supernews.com Original-Lines: 67 Original-Xref: shelby.stanford.edu gnu.emacs.help:112384 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:8883 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:8883 Oliver Scholz writes: > 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. ;) ... > 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)))))))) Hey, wow. (And you claim not to be a lisp guru! ;) It seems to work well for these two tests: recursion and mutual recursion. (my-simple-labels ((f (s n) (if (> n 0) (progn (insert (format "s: %s\n" s)) (f s (1- n)))))) (f "hi" 4)) Inserts "s: hi\n" four times. (my-simple-labels ((f (s n) (if (> n 0) (progn (insert (format "f: %s\n" s)) (g s (1- n))))) (g (s n) (if (> n 0) (progn (insert (format "g: %s\n" s)) (f s (1- n)))))) (f "hi" 4)) Inserts "f: hi\n" and "g: hi\n" alternately, twice each. -- --Ed L Cashin PGP public key: http://noserose.net/e/pgp/