Subject: Re: Trouble with labels From: Erik Naggum <erik@naggum.net> Date: 11 Apr 2001 12:58:10 +0000 Newsgroups: comp.lang.lisp Message-ID: <3195982690560792@naggum.net> * 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: