Page 1.        Your Name:______________________________________

Problem 1:

    A: To test the MIT cost-accounting database Dr. Writeoff enters the
       following expression:

       (lookup 'staff (lookup '6-182 (lookup 'subjects mit-operations)))

       What does the interpreter print as a result of this evaluation?
       Write your answer in the space provided below:












    B: What expression should Dr. Writeoff enter to find out from the
       database how many hours per week the room 10-250 is being used
       by 6.001?   
       Write your answer in the space provided below:









Page 2.        Your Name:______________________________________

Problem 2:

      Draw a box-and-pointer diagram that corresponds to the list
      structure whose printed representation is:

               (rooms (36-353 3) (36-744 10))

      Draw your answer in the space provided below:
















Page 3        Your Name:______________________________________

Problem 3:

   The procedure to accumulate a sum of a list of numbers was defined
   in the handout as 

        (define (add-up list-of-numbers)
          (if (null? list-of-numbers)
              0
              (+ (car list-of-numbers)
	         (add-up (cdr list-of-numbers)))))

   A: In the ADD-UP code shown above, if the LIST-OF-NUMBERS is of length 88,
      exactly how many times is the addition procedure (the value of the
      operator +) called?  Write your answer in the space provided below:







   B: Exactly how many times is the addition procedure (the value of the 
      operator +) called in the evaluation of the expression:

        (cost-of-subject (lookup '6-182 (lookup 'subjects mit-operations)))

      Write your answer in the space provided below:







   C: Mr. Bitdiddle proposes that it is neater to write the program 
      as follows, but only an incomplete version is available:

	(define (add-up list-of-numbers)
	  (define (iter to-go ans)
	    (if (null? <a>)
	        <b>
		(iter <c> <d>)))
	  (iter list-of-numbers 0))

      In the spaces provided below, write the expressions that should 
      be in the labeled places:

	<a>:


	<b>:


	<c>:


	<d>:

Page 4.        Your Name:______________________________________

Problem 4:

   Alyssa P. Hacker observes that there is significant opportunity to 
   simplify the code by judicious use of procedural abstractions.  For 
   example, she observes that one could combine the following two 
   procedures 

    (define (cost-of-staff subject-entry)
      (add-up (map cost-of-person
	           (table-entries (lookup 'staff subject-entry)))))

    (define (cost-of-space subject-entry)
      (add-up (map cost-of-room
	           (table-entries (lookup 'rooms subject-entry)))))

   by defining:

    (define (cost-of type each entry)
      (add-up (map each (table-entries (lookup type entry)))))

    (define (cost-of-staff subject-entry)
      (cost-of 'staff cost-of-person subject-entry))

    (define (cost-of-space subject-entry)
      (cost-of 'rooms cost-of-room subject-entry))

   A: COST-OF-SUBJECTS may also be defined in terms of the COST-OF
      abstraction.  Write such a definition of COST-OF-SUBJECTS in
      the space provided below:





  
Page 5.        Your Name:______________________________________

Problem 4 continued:

 B: There are 2 additional sets of procedures in the code supplied 
    with the announcement that can be easily expressed in terms of
    other procedural abstractions.  In the space provided below, give
    the names of two other pairs of procedures that can be expressed
    in such a fashion.

      Set1: Procedure1:

            Procedure2:


      Set2: Procedure1:

            Procedure2:


 C: For one of the two sets of procedures you chose in part B, 
    write an abstraction procedure and new definitions of the
    two procedures that you have listed, in the space provided 
    below:








Page 6.        Your Name:______________________________________

Problem 5.  After we expanded the database to include the entire
School of Engineering, it was observed that things got quite slow.
The LOOKUP procedure seems especially slow when chasing around in the
table giving the rank of each staff member.

LOOKUP is implemented with ASSQ; and ASSQ is implemented as follows:

(define (assq key alist)
  (cond ((null? alist) #f)
	((eq? key (caar alist)) (car alist))
	(else (assq key (cdr alist)))))

We are told that it is possible to represent a table in such a way
that access to the table is much faster than by using ASSQ.  In fact,
the people from the School of Science submitted their section of the
database in just such a form, which they call STABLES.  Unfortunately
the School of Science method is much worse than ASSQ for short tables.
ASSQ tables are best for short tables, and STABLES are good for long
tables.  Science submitted their data in the following form:

(define science-operations
  (make-stable ...
               ...
               ))

We want to make a system to allow both implementations of tables to be
used.  Ben Bitdiddle suggests that we use a generic table mechanism to
allow us to use the optimal table for each purpose.  He changed our
system to accommodate the alternative representations.  His
modifications are shown in the code fragment included with the quiz
(see the next page -- Page 7).

Unfortunately, our copy of Mr. Bitdiddle's proposal is somewhat
smudged.  In the spaces provided below, fill in the expressions
represented by the labeled smudges.

<smudge1>:



<smudge2>:



<smudge3>:



<smudge4>:



<smudge5>:
Page 7.            Ben Bitdiddle's Proposal
;;; We already have ASSQ tables in Scheme.  STABLES are defined below:

(define (build-stable-record-structure record-list)
  <School of science proprietary code>)	; This is not a smudge!

(define (stable-lookup key stable-record-structure)
  <School of science proprietary code>)	; This is not a smudge!


;;; Besides the table header, each table will have a tag describing 
;;; its representation type.  We modify the old ASSQ tables to include
;;; this tag.  

(define (make-table header entries)
  (cons header (cons assq-type entries)))

;;; STABLEs are distinguished from ASSQ tables by representation type.

(define (make-stable header list-of-records)
  (cons header
        (cons stable-type
              (build-stable-record-structure list-of-records))))


;;; The table types are defined as follows:

(define (assq-type message)
  (cond ((eq? message 'make)   <smudge1>)          ;#@!$%
	((eq? message 'lookup) assq)
	(else (error "Unknown message -- ASSQ-TABLE" message))))

(define (stable-type message)
  (cond ((eq? message 'make) make-stable)
	((eq? message 'lookup) <smudge2>)          ;&%$#@
	(else (error "Unknown message -- STABLE" message))))


;;; The following selectors work for both representations.

(define (table-header table)
  (car table))

(define (table-entries table)
  (cddr table))

(define (table-representation-type table)
  (cadr table))


;;; The LOOKUP procedure will do what is necessary for each kind of
;;; table.

(define (lookup key table)
  (let ((method	<smudge3>))		          ;$%#&@
    (let ((record (method <smudge4> <smudge5>)))  ;*&%$#
      (if record	                          ; false if key is absent
	  record
	  (error "Key not in table:"
                 key
                 (table-header table))))))
