Path: utzoo!attcan!uunet!mailrus!tut.cis.ohio-state.edu!ucbvax!pasteur!jwz@teak.berkeley.edu From: jwz@teak.berkeley.edu (Jamie Zawinski) Newsgroups: comp.lang.lisp Subject: Re: code Message-ID: <19727@pasteur.Berkeley.EDU> Date: 19 Nov 89 06:26:18 GMT Sender: news@pasteur.Berkeley.EDU Lines: 57 In-reply-to: <1989Nov18.152356.7962@hellgate.utah.edu> In article <1989Nov18.152356.7962@hellgate.utah.edu> Tim Moore writes: > If you want persistant lexical bindings you probably should be using closures > instead. Here's you fibonacci example, rewritten to use a closure: > [ incorrect version replaced: ] > (defun fib-setup (&key ((:n-2 pn-2) 0) ((:n-1 pn-1) 1)) > #'(lambda (&key n-2 n-1) > (and n-2 (setq pn-2 n-2)) > (and n-1 (setq pn-1 n-1)) > (psetq pn-2 pn-1 pn-1 (+ pn-2 pn-1)) > pn-1)) > FIB-SETUP > (setf (symbol-function 'fibonacci) (fib-setup)) > # > (fibonacci) > 1 > (fibonacci) > 2 > (fibonacci) > 3 > (fibonacci) > 5 This seems to me to be a more straightforward way: (let ((pn-2 0) (pn-1 1)) (defun fibonacci () (psetq pn-2 pn-1 pn-1 (+ pn-2 pn-1)) pn-1)) Since DEFUN must expand to something that stuffs a lambda into a function-cell, it's equivalent to yours, but is shorter and easier to read. A problem with this is, some less-swift compilers might not be smart enough to compile top-level lexical closures like this. (But that's wrong.) > Note: I too think it would be a good thing if people posted more code > and pointers to code. Ok, here's one. The directory /usr/jwz/public/ on spice.cs.cmu.edu is accessible by anonymous FTP. This directory contains 4 or 5 megs of Lisp code. Most of it (about 80% I think) is TI Explorer specific, but a fair amount is common lisp; there's a good chance that a lot of the Explorer-specific code will run on Symbolics, since they share common ancestors. Most of the machine dependancies are in user-interface or graphics stuff, so it might not be hard to port to other implementations. The file "_readme.text" contains short descriptions of all of the files, along with whether they are CL-compatible. The Spice's IP address is 128.2.254.139, for those of you with brain-dead nameservers. If you have any neat tools that you'd like to see become more accessible, send them to me. -- Jamie (jwz@teak.berkeley.edu or jwz@spice.cs.cmu.edu)