-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.46.scm
30 lines (26 loc) · 809 Bytes
/
1.46.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(define (iterative-improve good-enough? improve-guess)
(define (try guess)
(let ((next (improve-guess guess)))
(if (good-enough? guess next)
next
(try next))))
try)
; rewrite the sqrt procedure of 1.1.7 in terms of iterative-improve
(define (sqrt x)
(define (good-enough? guess)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess)
(define (average x y)
(/ (+ x y) 2))
(average guess (/ x guess)))
((iterative-improve good-enough? improve) 1.0))
; rewrite fixed-point procedure of 1.3.3 in terms of iterative-improve.
(define tolerance 0.00001)
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2))
tolerance))
((iterative-improve
(lambda (x) (close-enough? x (f x)))
f)
first-guess))