I also cheated slightly by skipping a couple of exercises (1.45 which I was just too tired to experiment with, and one of the math proofs)
Here's my programs for the final exercise
1.46:
(define (iterative-improve good-enough improve)
(lambda (guess)
(define (iter new-guess)
(if (good-enough new-guess)
new-guess
(iter (improve new-guess))))
(iter guess)))
(define (sqrt x)
(define (good-enough guess)
(display guess)
(newline)
(if (< (abs (- (square guess) x))
0.001)
true
false))
(define (improve guess)
(average guess (/ x guess)))
((iterative-improve good-enough improve) x))
for fixed point:
(define (fixed-point f)
(define tolerance 0.00001)
(define (good-enough guess)
(let ((new-guess (f guess)))
(if (< (abs (- new-guess guess)) tolerance)
true
false)))
(define (improve guess)
((average-damp f) guess))
(iterative-improve good-enough improve))
(define (average-damp f)
(lambda (x) (average x (f x))))
> ((fixed-point cos) 1.0)
0.7390885809390573
> (define (fixed-sqrt x)
((fixed-point (lambda (y) (/ x y)))
1.0))
> (fixed-sqrt 10)
3.162277665175675
It's been enjoyable, but considering the quantity of exercises, I really had to barge through it. I'm going to take the next week easier so I can review some of the lectures.
Posting next sundays target tomorrow.
No comments:
Post a Comment