solutions/Structure and Interpretatio.../sicp-ex-1.8.rkt

24 lines
508 B
Racket
Raw Permalink Normal View History

2024-04-16 19:13:00 -04:00
(define (square x) (* x x))
(define (cubic-iter guess guess-prev x)
(if (good-enough? guess guess-prev)
guess
(cubic-iter (improve guess x) guess
x)))
(define (improve guess x)
(average (* 2 guess) (/ x (square guess))))
(define (average x y)
(/ (+ x y) 3))
(define (good-enough? guess guess-prev)
(< (abs (- guess guess-prev)) 0.001))
(define (cubic-root x)
(if (= x 0) x (cubic-iter 1.0 999 x)))
(cubic-root 1892379832.0)
(cubic-root 8.0)
(cubic-root 27.0)