CISC280 Midterm 2, 16 Apr 2002, Name _______________________

  1. (6 points) Define proper list

    Answer: A proper list is the empty list (), or is a pair whose cdr is a proper list.
     

  2. (18 points) The following questions apply to the lists L = (1 2 3) and M = ((4 5) (6 7)).

     
    1. (append L M) returns ___ (1 2 3 (4 5) (6 7))
    2. (list L M) returns ___ ((1 2 3) ((4 5) (6 7)))
    3. (cons L M) returns ___ ((1 2 3) (4 5) (6 7))
    4. (cons (list L (+ 1 3)) (append (list M) M)) returns ___ (((1 2 3) 4) ((4 5) (6 7)) (4 5) (6 7))
    5. (cdr (cdr M)) returns ___ ()
    6. (length (list L M M M L)) returns ___ 5

     
  3. (8 points) Draw a box and arrow diagram of ((1 . 2) (3 4) 5).
     
    _______   _______   _______
    |__|__|-->|__|__|-->|__|_\|
     |         |         5
     |         |
    _v_____   _v_____   _______
    |__|__|   |__|__|-->|__|_\|
     1  2      3         4
    

     
  4. (8 points) Compound data types have, at the first level of abstraction, basic functions which are of one of four kinds: constructors, selectors, recognizers, and mutators, depending on their purpose. By each purpose below write the appropriate kind of basic function.

    1. __ selector _____ returning a part out of an object of the type.

    2. __ mutator ______ changing some part of the object.

    3. __ constructor __ creating a new object of the type.

    4. __ recognizer ___ determining that an object is an instance of the type as opposed to another type.

     
  5. (8 points) Say in English what the function no-fun does. Relate it to a course activity.
    (define (missing-filter a Types)
      (cond ((null? Types) '())
    	((get a (car Types)) (missing-Filter a (cdr Types)))
    	(else (cons (list a (car Types)) (missing-filter a (cdr Types)))) ))
    (define (no-fun L M)
      (apply append (map (lambda (x) (missing-filter x M)) L)))
    

    Answer: No-fun returns a list of key pair lists (l m) for which there is no entry in the table. This is yesterday's topper.
     

  6. (8 points) Pat Tern has decided to implement reverse as shown below. (Reverse L) produces a list which is a copy of the list L but with the entries in reverse order. The entries themselves are not reversed even if they happen to be lists.
    (define (evens L) (if (null? L) '() (cons (car L) (odds (cdr L)))))
    (define (odds L) (if (null? L) '() (evens (cdr L))))
    (define (reverse L)
      (cond ((or (null? L) (null? (cdr L))) L)
    	((even? (length L)) (interleave (reverse (odds L)) (reverse (evens L))) )
    	(else (interleave (reverse (evens L)) (reverse (odds L))) ) ))
    
    To complete the implementation, define (interleave L M) which takes two lists L and M It returns the list consisting of the first element of L followed by the first entry of M followed by the second entry of L, the second entry of M, etc. Assume that the lists L and M are of the same length or L has exactly one more entry than M.

    For example, (interleave '(a b c) '(1 2)) yields (a 1 b 2 c), (interleave '(a b c) '(1 2 3)) yields (a 1 b 2 c 3), and (interleave (list 9) '()) yields (9).

    Answer:

    (define (interleave L M) (if (null? M) L (cons (car L) (interleave M (cdr L)))))
    
  7. (10 points) Define a painter named tee to draw the letter "T" centered in the frame. The length of the vertical bar should be half the height of the frame and the top cross bar should be half the width of the frame in length.

    Answer:

    (define h1 (new-vect 1/4 3/4))
    (define h2 (new-vect 3/4 3/4))
    (define v1 (new-vect 1/2 1/4))
    (define v2 (new-vect 1/2 3/4))
    (define tee (segments-painter (make-segment h1 h2) (make-segment v1 v2)))
    
  8. (8 points) Show what this painter draws.
    (define this-painter (below (beside tee (flip-vert tee)) (box tee)))
    

    Answer: ...Check it out with DrScheme
     

  9. (10 points) Define a unary painter operator (squash p) which takes a painter p and returns a painter q which draws what p would draw but transformed to be scaled down to the bottom one-tenth of the frame (yet full width).

    Answer:

    (define (squash p) 
      (transform-painter p 
    		     (new-vect 0.0 0.0)
    		     (new-vect 1.0 0.0)
    		     (new-vect 0.0 0.1) ))
    
  10. (8 points) Consider these definitions of a higher order operator, a binary painter operator, and a painter.
    (define (make-mess upo bpo)
      (lambda (p1 p2) (below (upo p1) (bpo p1 p2))))
    (define miasma (make-mess squash beside))
    (define beauty (miasma tee X))
    
    Show what (beauty view) draws.

    Answer: ...Check it out with DrScheme
     

  11. (8 points) Suppose these definitions have been made.
    (define (apply-generic-one-arg op arg)
      ((get op (list (type-tag arg))) (contents arg)))
    (define (name-A untagged-A) (cadr untagged-A))
    (define (name-B untagged-B) (car untagged-B))
    (define (name-C untagged-C) (caar untagged-B))
    (put 'name '(A) name-A)
    (put 'name '(B) name-B)
    (put 'name '(C) name-C)
    
    Define a generic procedure (name tagged-record) which takes an object which is tagged with 'A or 'B or 'C and returns the name field out of that object. Use apply-generic-one-arg in your definition.

    Answer:

    (define (name tagged-record)
      (apply-generic-one-arg 'name tagged-record))