CS 22
Clab 34: Tracing in the Meta-circular evaluator
SICP standard evaluator
Create a week12 directory.
Copy all of /home/cfk/pub/cs22/week12 to your week12 directory.
We'll review how tracing capabilities can be added
to our evaluator (in neval11.scm) and then try the tracing
evaluator together on the
following:
(define x 40)
(define (square x) (* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (f a)
(sum-of-squares (+ a 1) (* a 2)))
(define (factorial n)
(cond
((= n 1) 1)
(else (* n (factorial (- n 1))))
)
)
(define (newfact n)
(fact-iter 1 1 n))
(define (fact-iter product counter max-count)
(cond
((> counter max-count) product)
(else (fact-iter (* counter product)
(+ counter 1)
max-count))
))
;;; need begin and >= to make this example work
(define (make-withdraw balance)
(lambda (amount)
(cond
((>= balance amount)
(begin
(set! balance (- balance amount))
balance))
(else 'insufficient))))
(define w1 (make-withdraw 100))
(w1 50)
(define w2 (make-withdraw 100))
(w2 20)
(w1 20)
Now let's take a look at the standard SICP evaluator together
(mceval.scm).
Then try it on a few normal things like those above that
we traced. Now remember where the break key is and try it on:
(define (try a b)
(if (= a 0) 1 b))
(try 0 'cat)
(try 1 'cat)
(try 0 (/ 5 0))
(define (unless condition usual-value exceptional-value)
(if condition exceptional-value usual-value))
(define (factorial n)
(unless (= n 1)
(* n (factorial (- n 1)))
1))
(factorial 5)
(define (improve guess x)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
(define (sqrt-iter guess x)
(new-if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
(define (sqrt x)
(sqrt-iter 1.0 x))
(define (square x) (* x x))
(sqrt 25)
Why don't these things work in mceval? Will they work in drscheme?