C311 Spring 1996: Exam 2

Haynes, Hilsdale, and Subramaniam

April 1, 1996

You have the entire class period for this exam. Show your work to receive credit. If you use any procedure that is not standard in Scheme, you must define it (in any blank space that seems large enough).


  1. 15 points
    What is the output of the following Scheme expressions?
    (+ 3 4 (call/cc (lambda (k) 
                      (* 80 (k 3))))
           7 5)
    
    Answer: 22
    
    (+ 3 4 (call/cc (lambda (k) 
                      (* 80 (k (k 3)))))
           7 5)
    
    Answer: 22
    
    (+ 3 4 (call/cc (lambda (k) 
                      (+ 6 (call/cc (lambda (j)
                                       (* 80 (j (k 3))))))))
           7 5)
    
    Answer: 22
    
  2. 15 points
    Convert the following procedure into continuation-passing style.
    (define Y
      (lambda (m)
        ((lambda (f)
           (f f))
         (lambda (f)
           (m (lambda (x) ((f f) x)))))))
    
    Answer:
    
    (define Y-cps
      (lambda (m k1)
        ((lambda (f k2)
           (f f k2))
         (lambda (f k3)
           (m (lambda (x k4)
    	    (f f (lambda (v)
    		   (v x k4))))
    	 k3))
         k1)))
    
  3. 50 points
    The procedure length takes a list ls and returns the length of the list:
    (define length
      (lambda (ls)
        (if (null? ls)
            0
            (add1 (length (cdr ls))))))
    
    (length '(a b c)) ==> 3
    (length '())      ==> 0