1.7
(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))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (square x) (* x x))
(sqrt-iter 1 0.0001)
(sqrt 0.0001)
=
(- 0.03230844833048122 (sqrt 0.0001))
=
(/ (- 0.03230844833048122 (sqrt 0.0001)) 0.0001)
Design and Implementation of better sqrt-iter
:
(define (sqrt-iter lastGuess guess x)
(if (good-enough? lastGuess guess)
guess
(sqrt-iter guess (improve guess x) x)
)
)
(define (good-enough? lastGuess guess)
(< (abs (- lastGuess guess)) 0.001)
)
Compare to the previous example:
(sqrt-iter 1 100 0.0001)
(sqrt 0.0001)
=
(- 0.010028540197249 (sqrt 0.0001))
=
(/ (- 0.010028540197249 (sqrt 0.0001)) 0.0001)