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.
((lambda (x)
((lambda ()
((lambda (y)
(lambda (w z) (+ w x y z)))
5))))
4)
(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.
(define (f x) (+ 3 (* 2 x))) (define (g y z) (+ (* y y) (* z z))) (f (g (f 7) 8))
(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))]))
;; 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)))]))
(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)))]))
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.
Refer to the Laws of Homework when preparing your solutions.