問題1.8, p.14

サンプルコード

;code/problem-1-8.scm

(define (cbrt-iter guess pre-guess x)
    (if (good-enough? guess pre-guess)
        guess
        (cbrt-iter (improve guess x) guess x)))

(define (improve guess x)
    (/ (+ (/ x (* guess guess)) 
                (* 2 guess))
            3))

(define (good-enough? guess pre-guess)
    (< (/ (abs (- guess pre-guess)) guess) 0.0001))

(define (cbrt x)
    (cbrt-iter 1.0 100.0 x))

#?=(cbrt 8)
#?=(cbrt 27)
#?=(cbrt 0.000002)
#?=(cbrt 200000000000000000000)

実行結果

#?="./problem-1-8.scm":19:(cbrt 8)
#?-    2.000000000012062
#?="./problem-1-8.scm":20:(cbrt 27)
#?-    3.0000000000000977
#?="./problem-1-8.scm":21:(cbrt 2.0e-6)
#?-    0.012599210498948799
#?="./problem-1-8.scm":22:(cbrt 200000000000000000000)
#?-    5848035.476470061