                                                             PAGE 1 of  10

                                                      GO ON TO THE NEXT PAGE                                                  

                                                             PAGE 1 of  10

                                                                                                


MASSACHUSETTS INSTITUTE OF TECHNOLOGY
Department of Electrical Engineering and Computer Science
6.001Structure and Interpretation of Computer Programs
October 22, 1990
Quiz 1
____________________

Closed book, except for one page of notes
_____________________


All answers should be written in the spaces provided.  These are the only responses that will be 
graded.



Your name: ____________________________________________________________


Problem 0: (1 point)

(a) Circle the name of your recitation instructor:


Joel Moses           Jerry Saltzer             Peter Szolovits            Lynn Stein


(b)  Circle the name of your tutor:


      Orca Starbuck             Steven Davis           Mark Day            David Goldstone          Tom Simon


       Ziggy Blair               Kai-Yee Ho             Barbara Gates       Hal Abelson           Eric Grimson


Tom Greene              Joel Moses           Jerry Saltzer             Peter Szolovits            Lynn Stein


   

PROBLEM 1: (30 points)

Consider (as in the tutorial exercises for problem set 2) the task of representing sets of 
playing cards.  Each card has a face-value and a suit.  We can define a card as the following 
data abstraction:

 make-card takes a face-value and a suit as arguments and returns a card
 card-face-value takes a card as argument and returns the card's face value
 card-suit takes a card as argument and returns the card's suit

To represent sets of cards, Ben Bitdiddle proposes the a card-set data abstraction:

 card-set-first-card takes a card set as argument and returns the first card in the set
 card-set-rest-cards takes a card set as argument and returns the rest of the cards in the set
 card-set-empty? tests whether a card set is empty
 card-set-adjoin-card takes a card and a card set and returns a new card set.  This new set 
         contains the cards of the given set, and also the new card.
 card-set-empty-card-set is a name for the empty set of cards

Ben suggests representing a card set simply as a list of cards, and gives the following 
implementation of card sets:

(define ben-card-set-first-card car)
(define ben-card-set-rest-cards cdr)
(define ben-card-set-empty? null?)
(define (ben-card-set-adjoin card card-set) (cons card card-set))
(define ben-card-set-empty-card-set '())

Alyssa P. Hacker suggests  an alternate representation for a card set as a pair of lists: a list of face 
values and a list of corresponding suits.  Thus, the second card in the set (for example) would be 
the card whose face value is the second item in the list of face values, and whose suit is the second 
item in the list of suits.  Here is Alyssa's definition of card-set-first-card for this representation:

(define (alyssa-card-set-first-card card-set)
  (make-card (car (car card-set)) (car (cdr card-set))))



PROBLEM 1 (cont):

(a)  Fill in the blanks to complete the definition of Alyssa's card set representation.  (You 
may not need to use all of the blank lines we've provided here; but if you need more space 
than that, you are probably doing the wrong thing.)


(define (alyssa-card-set-rest-cards card-set)

     __________________________________

     __________________________________  )


(define (alyssa-card-set-empty? card-set)

    ___________________________________

    ___________________________________ )



(define (alyssa-card-set-adjoin card card-set)
    
     ___________________________________

     ___________________________________ 

     ___________________________________   )


(define alyssa-card-set-empty-card-set
    
      ___________________________________  )


(b) Complete the definition of the following procedure, which transforms a set of cards in 
Ben's representation, to the same set of cards in Alyssa's representation:


(define (ben-card-set->alyssa-card-set card-set)

  (______________

      (mapcar _______________________ card-set)


      (mapcar _______________________ card-set)))


Your answers to all parts of this problem so far should make no assumptions about how 
individual cards are represented, other than through the card data abstraction provided.  


PROBLEM 1 (cont):

Suppose now that we represent card face values using the numbers 2 through 10 and the 
symbols JACK, QUEEN, KING, ACE; that we represent suits using the symbols 
CLUBS, DIAMONDS, HEARTS, SPADES.  Suppose also that we represent a card as a 
two-element list whose first element is the face value and whose second element is the suit:

(define (make-card face-value suit)
   (list face-value suit))


(c) Draw the box and pointer structure for the card set consisting of the jack of hearts and 
the 3 of clubs (in either order), using Ben's representation for cards sets.











(d) Draw the box and pointer structure for the same card set, using Alyssa's representation 
for card sets.











Louis Reasoner claims that the data structure in part (d) can be regarded as an ordinary list.  
For example, he says, we can find the length of this list using the usual length procedure:

(define (length lst)
  (if (null? lst)
      0
      (+ 1 (length (cdr lst)))))

(e) What value is returned when Louis uses this procedure to compute the length of the data 
structure in part (d):

_______ 0      _____   1          ______ 2          _______ 3       ________ 4

_______  error (because this is not a proper list)

______ nothing (the procedure goes into an infinite loop)

 
PROBLEM 2:  (10 points)

 One way to define the fibonacci function is as follows:  Except for the case when n is less than 2, 
fib(n) is fib(n1)+fib(n2).   Otherwise fib(n) is n.  Louis Reasoner attempts to create a Scheme 
procedure that computes fibonacci numbers in this way.  He makes use of a new conditional 
expression called except-for, which he defines as follows:

(define (except-for exception usual-result exception-result)
   (if (not exception)
       usual-result
       exception-result))

He now defines fib, and also a test procedure to test his new conditional:
                     
(define (test n)
   (except-for (= n 0)
               'non-zero
               'zero))
              
(define (fib n)
   (except-for (< n 2)
               (+ (fib (- n 1)) (fib (- n 2)))
               n))


(a) What value (if any) is returned when Louis evaluates   (test 3)  ?





(b) What value (if any) is returned when Louis evaluates (fib 0)   ?






(c) What value (if any) is returned when Louis evaluates (fib 2)  ?

PROBLEM 3: (10 points)

Consider the following procedure definitions:

(define compose
  (lambda (f g)
    (lambda (x) (f (g x)))))


(define try
   (lambda (f n)
     (if (= n 1)
         f
         (compose f (try f (- n 1))))))


Precisely one of the following expressions returns a number when evaluated.  Indicate 
which expression, and show what number it returns:

______ (try (* x 10) 5)


______ (try (lambda (x) (* x 10)) 5)


______ ((try (lambda (x) (* x 10)) 3) 5)


______ (try compose 5)


______ ((try compose 5) 3)



Number returned:   ________________________

PROBLEM 4: (10 points)

Problem set 3 dealt with a graphics language in which a "picture" is represented as a 
procedure that takes a triangle as input and draws a design scaled to fit in the triangle.  In 
that problem set, you implemented various means of combination pictures.  Illustrated 
below is a means of combination called superimpose.  Superimpose takes two pictures 
as arguments and produces a new picture.  This new picture draws both of the original 
pictures in the designated triangle.



                                       

Complete the following definition of superimpose:

(define (superimpose pict1 pict2)













                                        

PROBLEM 5: (10 points)

The code for problem set 4 used a procedure sublist that substitutes for the elements in a 
list as specified by a list of replacements:

==> (sublist '((i you) (me you) (am are) (my your) (your my))
                           '(i  am not so sure your advice is sound)    )
returns
    (you are not so sure my advice is sound)

Here is the implementation of sublist, using another procedure, substitute:

(define (sublist replacements list)
  (mapcar (lambda (elt) (substitute replacements elt))
          list))

(define (substitute replacements item)
  (cond ((null? replacements) item)
        ((eq? item (caar replacements)) (cadar replacements))
        (else (substitute (cdr replacements) item))))

Show how to write a procedure subtree similar to sublist, but that substitutes for the 
elements in a tree, represented as a list of lists:

==> (subtree '((i you) (me you) (am are) (my your) (your my))
                           '(i  (am) (not (so) ((sure your)) advice) is sound)  )
should return
    (you (are) (not (so) ((sure my)) advice) is sound)


Complete the following definition:

(define (subtree replacements tree)
    (cond ((null? tree)  _____________________________________________ )
       
          ((atom? tree) _______________________________________________)

          (else (cons   _______________________________________________

                        _______________________________________  ) ) ) )


Complete the following alternative definition of subtree

(define (subtree replacements tree)
    (cond ((null? tree)  _____________________________________________ )
       
          ((atom? tree)  _____________________________________________ )


          (else (mapcar ________________________________________________

                                                          
                        ________________________________________________ 

                        ) ) ) )


PROBLEM 6: (10 points)

In problem set 3, you wrote a procedure to find the last element of a list.  Here are three 
possible implementations:

(define (last-1 lst)
  (if (null? (cdr lst))
      (car lst)
      (last-1 (cdr lst))))

(define (last-2 lst)
  (nth (- (length lst) 1) lst))

(define (last-3 lst)
  (if (= (length lst) 1)
      (car lst)
      (last-3 (cdr lst))))

The subprocedures nth and length (used in last-2 and last-3) are defined as in the 
book:

(define (nth n lst)
  (if (= n 0)
      (car lst)
      (nth (- n 1) (cdr lst))))

(define (length lst)
  (if (null? lst)
      0
      (+ 1 (length (cdr lst)))))


(a) Alyssa P. Hacker claims that these three procedures will take noticeably different 
amounts of time on long lists.  Rank order  the three procedures, from fastest to slowest:

_____ fastest

_____middle

_____slowest

(b) For each of the three procedures, indicate the order of growth in time, as a function of 
the length of the list.  You may choose from O(1), O(log n), O(n), O(n^2), O(2^n)

last-1:

last-2:

last-3:


EXTRA CREDIT: (1 point)

To which of the following characters associated with 6.001 is there a seat dedicated in the 
10250 lecture hall?

___ Hal Abelson                 ___ Eric Grimson           ____ Gerry Sussman

___ Ben and Alyssa P. (Hacker)  Bitdiddle                ____ Louis Reasoner   




THIS IS THE END OF THE QUIZ


