HW7: Scheme Tracing
Due: Monday 4/6/2009
You should do this by yourself. This is not a group project.
If you want you can program these into the JScheme interpreter
and see if your final answer is correct...
Trace the evolution of the following Scheme programs:
-
(+ (* 2 (+ 3 (* 4 5) 6) 7) 8)
-
(define (sq x) (* x x)) (define (f L) (sqrt (+ (sq (first L)) (sq (second L))))) (f '(3 4))
-
(define (g L n) (if (null? L) n (if (even? (first L)) (g (rest L) n) (g (rest L) (+ n (first L)))))) (g '(1 2 3 4 5) 0) -
(define (h x y z) (if (< x 1) z (h (- x 1) y (* y z)))) (h 5 2 1)
-
(define (f x y) (if (= x 0) y (f (- x 1) (+ y 1)))) (f 2 5) (f -2 7)
-
(define (p x y z) (if (= x 0) z (if (even? x) (p (/ x 2) (* y y) z) (p (- x 1) y (* y z)) ))) (p 5 2 1)
