-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.46.scm
34 lines (25 loc) · 872 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
31
32
33
34
(define (iterative-improve good-enough? improve)
(lambda (guess)
(if (good-enough? guess)
guess
((iterative-improve good-enough? improve) (improve guess)))))
(define (sqrt x)
(define (average a b)
(/ (+ a b) 2))
(define (square x)
(* x x))
(define (good-enough? guess)
(let ((tolerance 0.00001))
(< (abs (- (square guess) x)) tolerance)))
(define (improve guess)
(average guess (/ x guess)))
((iterative-improve good-enough? improve) 1.0))
(sqrt 9) ; 3.000000001396984
(define (fixed-point f guess)
(define (close-enough? a b)
(let ((tolerance 0.00001))
(< (abs (- a b)) tolerance)))
(let ((good-enough? (lambda (x) (close-enough? x (f x))))
(improve f))
((iterative-improve good-enough? improve) guess)))
(fixed-point cos 1.0) ; 0.7390893414033927