CS 2135 (A01) Homework 5: Scoping and Continuations

Due: October 4 (Thursday) at 11:59pm via turnin (assignment name hwk5).

Assignment Goals


The Assignment

For each closure created while evaluating each of the following programs, indicate the contents of the environment for that closure. Use a format like

(lambda (x) ...) has env (a = 1) (b = 9) etc.
  1. ((lambda (x)
       ((lambda () 
          ((lambda (y)
             (lambda (w z) (+ w x y z)))
           5))))
      4)
    
  2. (define (func1 f x)
      (f (lambda (x) (+ x 5))
         (* x x)))
    
    (func1 (lambda (func2 x)
             ((lambda (z) (func2 (+ x z))) 7))
           3)
    

Convert each of the following programs into continuation-passing-style.

  1. (define (f x) (+ 3 (* 2 x)))
    (define (g y z) (+ (* y y) (* z z)))
    (f (g (f 7) 8))
    
  2. (define (square x) (* x x))
    (define (tri-height x) (sqrt (- (square x) (square (* .5 x)))))
    ;; assume sqrt is built-in
    
    (define (shape-area shape-type size)
      (cond [(symbol=? shape-type 'circle) (* pi (square size))]
            [(symbol=? shape-type 'square) (square size)]
            [(symbol=? shape-type 'equilat-tri) (* size (tri-height size))]))
    
  3. ;; assume that the argument f is user-defined and could be rewritten
    ;;   as f/k, taking a continuation as a second argument
    
    (define (map f L)
      (cond [(empty? L) empty]
            [(cons? L) (cons (f (first L))
                             (map f (rest L)))]))
    
  4. (define (arith-interp aexp)
      (cond [(number? aexp) aexp]
            [(plus? aexp) (+ (arith-interp (plus-left aexp))
                             (arith-interp (plus-right aexp)))]
            [(mult? aexp) (* (arith-interp (mult-left aexp))
                             (arith-interp (mult-right aexp)))]))
    

What to Turn In

Turn in a single file hwk5.ss (or hwk5.scm) containing all code and documentation for this assignment. Make sure that both students' names are in a comment at the top of the file.


Hints and Guidelines


Back to the Assignments page