CS341, Spring 1996
LISP Quiz
4/25/93
'(a (b . c) (d e f))
(setf x 3)
(defun f (x)
(setf x (- x 1))
(let ((z 4))
(setf y (* z 2))
(+ (g x) (g y) (g z))))
(defun g (y)
(format t "Y is ~a~%" y)
(setf y (* y 2))
y)
(f 2) is being
evaluated?(f 2) return?x, y, and z bound
to after the call completes?
(print-negative-numbers list) that
prints all the negative elements in the input list.
(ascending-order-p list-of-numbers) that
returns non-NIL iff its list is in ascending order. Examples:
(ascending-order-p '(3 5 9 10)) => T (ascending-order-p '(3 5 9 9 10)) => NIL
Suppose we have bank accounts and a bank like in the problem set:
(defstruct account number balance pin) (defstruct bank name accounts)Now consider the problem of how to pay interest on the accounts in the bank. To pay interest of 5% (0.05), one just increases the account's balance by a factor of 1.05:
.
For security reasons the bank does not want to allow direct access to the account balance, so we will pay interest as follows.
(account-numbers a-bank) that
returns a list of all account numbers associated with this bank.
(find-account a-bank account-number)
that takes a bank and an account number as input and returns the
account structure associated with that number, or NIL if
there is no such account. You can assume account numbers are integers.
(make-interest-payer a-bank account-number)
which returns a function. The returned function will take one
argument, an interest factor, and when called will increase the account balance
of that account by the specified factor. (As above, if the argument is
, the account's balance is increased by a factor of 1.05.)
You can use find-account in writing this function.
(pay-interest a-bank amount) that pays
interest in the specified amount to all accounts in the bank. You
must use the functions account-numbers and make-interest-payer
in your solution.
Just to illustrate, this is what your solution should do:
USER(1): (setf *bank*
(make-bank :accounts (list (make-account :number 1 :balance 100)
(make-account :number 2 :balance 200))))
#S(BANK :NAME NIL
:ACCOUNTS
(#S(ACCOUNT :NUMBER 1 :PIN NIL :BALANCE 100)
#S(ACCOUNT :NUMBER 2 :PIN NIL :BALANCE 200)))
USER(2): (pay-interest *bank* 0.1)
NIL
USER(3): (account-balance (first (bank-accounts *bank*)))
110.0
USER(4): (account-balance (second (bank-accounts *bank*)))
220.0