23 lines
425 B
Scheme
23 lines
425 B
Scheme
|
(define (sqrt_iter new_guess last_guess x)
|
||
|
(if (good_enough? new_guess last_guess)
|
||
|
new_guess
|
||
|
(sqrt_iter (improve new_guess x) new_guess x)
|
||
|
)
|
||
|
)
|
||
|
|
||
|
(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))
|