29 lines
598 B
Scheme
29 lines
598 B
Scheme
;this program does not work, it will cause an infinite loop
|
|
|
|
(define (sqrt_iter new_guess last_guess x)
|
|
(new-if (good_enough? new_guess last_guess)
|
|
new_guess
|
|
(sqrt_iter (improve new_guess x) new_guess x)
|
|
)
|
|
)
|
|
|
|
(define (new-if predicate then-clause else-clause)
|
|
(cond (predicate then-clause)
|
|
(else else-clause)))
|
|
|
|
(define (improve guess x)
|
|
(/ (+ guess (/ x guess)) 2))
|
|
|
|
|
|
(define (good_enough? guess last_guess)
|
|
(< (absolute (- guess last_guess)) 0.000000000000001))
|
|
|
|
(define (absolute x)
|
|
(if (< x 0)
|
|
(- 0 x)
|
|
x))
|
|
|
|
(define (sqrt x) (sqrt_iter (/ x 2.0) x x))
|
|
|
|
(print (sqrt 10))
|