Wednesday, March 25, 2009

make-balance function

I stumbled on this page and started to solve the problems, Here is solution to the ex3.
Define (make-balance initial-balance) to return a function that takes 0 or 1 arguments. If that function is called with 0 arguments, it returns the current balance. If called with 1 argument, which should be a number, it adds that number to the current balance, and returns the new balance.
> (setq bal (make-balance 100))
<>
> (funcall bal 10)
110
> (funcall bal -50)
60
> (funcall bal)
60
Solution:
(defun make-balance (bal)
(let ((balance bal))
#'(lambda (&optional (amt 0))
(setf balance (+ amt balance)))))

No comments:

Post a Comment