sicp-exs/sicp-ex-1.7.rkt

24 lines
483 B
Racket
Raw Normal View History

2020-07-19 19:40:30 +02:00
(define (sqrt-iter guess guess-prev x)
(if (good-enough? guess guess-prev)
guess
(sqrt-iter (improve guess x) guess
x)))
(define (improve guess x)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
(define (good-enough? guess guess-prev)
(< (abs (- guess guess-prev)) 0.001))
(define (sqrt-new x)
(if (= x 0) x (sqrt-iter 1.0 999 x)))
(sqrt-new 16)
(sqrt-new 1.0004)
(sqrt-new 1)
(sqrt-new 134895724398)
(sqrt-new 0)