On Tue, 10 Apr 2001 ·······@hotmail.com wrote:
>
> How can I make a function defined in labels refer to another function defined
> in the same labels body?
Remember, this isn't Scheme, but Common Lisp:
(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))
Also, the syntax for invoking the dispatch function isn't as compact as it
is in Scheme e.g.:
(funcall (funcall acct 'withdraw) 69.00)
Tim
> 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 gets an error:
> ; MAKE-WITHDRAW
> ;;;*** Warning in (SUBFUNCTION (LABELS DISPATCH) MAKE-ACCOUNT): WITHDRAW
> assumed special
> ;;;*** Warning in (SUBFUNCTION (LABELS DISPATCH) MAKE-ACCOUNT): DEPOSIT
> assumed special
> ;;;*** Warning in MAKE-ACCOUNT: DISPATCH assumed special
> ; MAKE-ACCOUNT
>
>
>
>