From ... Path: supernews.google.com!sn-xit-03!supernews.com!nntp.cs.ubc.ca!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!uio.no!Norway.EU.net!not-for-mail From: Erik Naggum Newsgroups: comp.lang.lisp Subject: Re: Trouble with labels Date: 11 Apr 2001 12:58:10 +0000 Organization: Naggum Software, Oslo, Norway Lines: 71 Message-ID: <3195982690560792@naggum.net> References: <4sf6dto7mf556cvahss96e5u1087idd6vq@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: oslo-nntp.eunet.no 986993890 25843 195.0.192.66 (11 Apr 2001 12:58:10 GMT) X-Complaints-To: newsmaster@eunet.no NNTP-Posting-Date: 11 Apr 2001 12:58:10 GMT Mail-Copies-To: never User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7 Xref: supernews.google.com comp.lang.lisp:9194 * iscaris@hotmail.com > How can I make a function defined in labels refer to another function > defined in the same labels body? The same way you refer to functions everywhere else in Common Lisp. I honestly do not understand why you are not using a Scheme system to learn Scheme when you don't know Common Lisp well enough to know how to refer to functions. Perhaps you think that Scheme is a Lisp and Common Lisp is a Lisp, so it should work? Just because two human languages have Indo-European roots doesn't mean that they can be interchanged if you want to learn one of them, either. > For example (this is from SICP page 223): > (defun make-account (balance) > (labels ((withdraw (amount) > (if (>= balance amount) > (progn > (setf balance (- balance amount)) > balance) > (format t "Insufficient funds"))) > (deposit (amount) > (setf balance (+ balance amount)) > balance) > (dispatch (m) > (cond > ((equal m 'withdraw) withdraw) > ((equal m 'deposit) deposit) > (t (format t "Unknown request -- MAKE-ACCOUNT"))))) > dispatch)) This is what object-oriented programming looks like in Scheme, with message-passing as the dispatch paradigm, using closures to hold the data. If you want to understand what it does, stick to Scheme and learn how it works in Scheme. Then see if the same concept, once understood, is applicable anywhere else and whether it may be grafted into any other system. (IMNSHO, it neither can nor should be.) Scheme and Common Lisp are extremely different languages. You should not assume that you can make Scheme code become Common Lisp code with a few minor changes. For instance, set! in Scheme is defined not to have a useful value. setf in Common Lisp is defined to have the value of the last value. This means that any Common Lisp programmer will look at your code and wonder why you're doing what you're doing, if there's something special going on. And why use equal with symbols? I briefly wondered whether that meant that m could be a string. What use is writing out error messages that don't even say what's wrong? This (among a host of other things) tells me that the whole example is designed to be used only as a simple toy to illustrate something, just as Scheme is. Here's how I would have written a similarly skeletal example in (real) Common Lisp: (defclass account () ((balance :initarg :deposit :initform 0 :accessor balance))) (defmethod withdraw ((account account) amount) (decf (balance account) amount)) (defmethod deposit ((account account) amount) (incf (balance account) amount)) (defmethod withdraw :before ((account account) amount) (unless (<= amount (balance account)) (error "~A has insufficient funds to withdraw ~A." account amount))) #:Erik -- I found no peace in solitude. I found no chaos in catastrophe. -- :wumpscut: