Spring 1996

c311 Final Exam

This exam is worth 100 points. You have two hours to answer six questions. Good luck!

  1. Continuation-Passing Style (15 points)
    CPS the following program. You may assume that the procedures null?, pair?, cons, car and cdr are primitive. All other procedures are potentially recursive and must take continuations.
    (define p
      (lambda (f a b)
        (f
          (letrec 
            ([f (lambda (x)
                  (cond
                    [(null? x) '()]
                    [(pair? (car x)) (f (cons (f (car x)) (f (cdr x))))]
                    [else (cons (car x) (f (cdr x)))]))])
            
            (f a))
          b)))
    
    Solution:
    (define p
      (lambda (f1 a b k1)
        (letrec 
          ([f2 (lambda (x k2)
    	     (cond
    	       [(null? x) (k2 '())]
    	       [(pair? (car x)) 
    		(f2 (car x)
    		  (lambda (v1)
    		    (f2 (cdr x)
    		      (lambda (v2)
    			(f2 (cons v1 v2) k2)))))]
    	       [else
    		 (f2 (cdr x)
    		   (lambda (v3)
    		     (k2 (cons (car x) v3))))]))])
            
          (f1 a
    	(lambda (v4)
    	  (f1 v4 b k1))))))
    
  2. Understanding Continuations (5 points)
    What is the output of the following Scheme program?
    (+ 6
      (call/cc (lambda (x)
                 (x (+ 5 (x (+ 3 4))))))
      7)
    
    Solution: 20
  3. Understanding Continuations (continued) (5 points)
    What is the output of the following Scheme program?
    ((call/cc (lambda (k)
                (k (lambda (v)
                     ((lambda (w) (+ w v))
                      v)))))
     10)
    
    Solution: 20
  4. Program Transformation (35 points)
    The procedure xerox takes a number n and and item x and returns a list with n copies of x.
    (define xerox
      (lambda (n x)
        (if (zero? n)
            '()
            (cons x (xerox (sub1 n) x)))))
    
    (xerox 3 'a) ==> (a a a)
    (xerox 0 'a) ==> ()
    
  5. Parameter-Passing Mechanisms (25 points)
    What is the output of the following Scheme program under the following parameter-passing mechanisms: a) call by value, b) call by reference, c) call by value result?
    (define prog
      (lambda ()
        (let ([x 1] [y 2])
          (let ([f (lambda (a b)
                     (writeln a)
                     (writeln b)
                     (writeln x)
                     (writeln y)
                     (set! a (+ a b))
                     (writeln a)
                     (writeln x)
                     (set! a (+ a a))
                     (set! b (+ b b))
                     (writeln (+ a b x y)))])
            (writeln x)
            (writeln y)
            (f x y)
            (writeln x)
            (writeln y)))))
    
    
    Solution:
    Value   Reference   Value-result    
    1	1           1                   
    2	2           2                   
    1	1           1                   
    2	2           2                   
    1	1           1                   
    2	2           2                   
    3	3           3                   
    1	3           1                   
    13	20          13                  
    1	6           6                   
    2	4           4                   
    
  6. Object-Oriented Style (15 points)
    In a particular Java program, only two classes are defined:
    class Rent {
      public void fries()
        System.out.println("Cpl. Agarn");
        return;
      }
      public void netfries()
        this.fries();
        return;
      } 
    }
    
    class Chillun extends Rent {
      public void fries() {
        System.out.println("Sgt. O'Rourke");
        super.fries();
        return;
      }
    }
    
    What is printed out when the following block is evaluated?
    { 
       Rent    jane      = new Rent();
       Chillun parmenter = new Chillun();
      
       jane.fries();
       parmenter.fries();
       jane.netfries();
       parmenter.netfries();
    }
    
    Solution:
    Cpl. Agarn
    Sgt. O'Rourke
    Cpl. Agarn
    Cpl. Agarn
    Sgt. O'Rourke
    Cpl. Agarn
    
  7. Using Continuations (5 points Extra Credit)
    Write the procedure loop-with-break. loop-with-break takes a procedure p, which takes one argument, break, and repeatedly applies p to some procedure of no arguments which, when invoked, will break out of the loop. loop-with-break may return anything you like (it returns the symbol *whatever* in the following example).
    > (let ((x 5))
        (loop-with-break
          (lambda (break)
            (set! x (sub1 x))
            (if (zero? x)
                (break))
            (displayln x))))
    4
    3
    2
    1
    *whatever*
    > 
    
    Solution:
    (define loop-with-break
      (lambda (p)
        (call/cc
          (lambda (k)
            (letrec ([loop (lambda ()
                             (p (lambda () (k '*whatever*)))
                             (loop))])
              (loop))))))
    
    or
    
    (define loop-with-break
      (lambda (p)
        (call/cc
          (lambda (k)
    	(p (lambda () (k '*whatever*)))
    	(loop-with-break p)))))