CS2a/Spr09: Introduction to Computers
HW7 - Scheme Tracing

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:
  1. (+ (* 2 (+ 3 (* 4 5) 6) 7) 8)
    
  2. (define (sq x) (* x x))
    (define (f L) (sqrt (+ (sq (first L)) (sq (second L)))))
    (f '(3 4))
    
  3. (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)
    
  4. (define (h x y z)
      (if (< x 1) z (h (- x 1) y (* y z))))
    (h 5 2 1)
    
  5. (define (f x y) (if (= x 0) y (f (- x 1) (+ y 1))))
    (f 2 5)
    (f -2 7)
    
  6. (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)
    
Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.0 Generic License