next up previous
Next: About this document

CS341, Spring 1996
LISP Quiz
4/25/93

  1. (10 points) Draw the box-and-arrow diagram associated with this list:
      '(a (b . c) (d e f))
    

  2. (15 points) Variable bindings:
    Consider the following code:
    (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)
    

    1. (5 points) What will be printed while (f 2) is being evaluated?
    2. (5 points) What does the call to (f 2) return?
    3. (5 points) What are the variables x, y, and z bound to after the call completes?

  3. (15 points) Write a function (print-negative-numbers list) that prints all the negative elements in the input list.

  4. (20 points) Write a function (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
    

  5. (40 points) Structures and closures:

    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.

    1. Write a function (account-numbers a-bank) that returns a list of all account numbers associated with this bank.

    2. Write a function (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.

    3. Write a function (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.

    4. Write a 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
    



    Do all your work for this problem on the next page.

    Work for Problem 5 on this page







next up previous
Next: About this document



Dave Grove
Tue Apr 23 10:44:24 PDT 1996