-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_07.scm
30 lines (28 loc) · 824 Bytes
/
1_07.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 (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
(define (square x) (* x x))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(define (improve guess x)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
; Inaccuracy examples:
;(sqrt 0.0001)
;(sqrt 9999999999999999999999999999999)
(define (good-enough? new-guess guess)
(let ((change (abs (- new-guess guess))))
(< change 0.001)))
(define (sqrt-iter guess x)
(let ((new-guess (improve guess x)))
(if (good-enough? new-guess guess)
guess
(sqrt-iter new-guess x))))
(define (sqrt x)
(sqrt-iter 1.0 x))
(sqrt 0.0001)
(sqrt 9999999999999999999999999999999)
; New method works better for the above examples.