summaryrefslogtreecommitdiffstats
path: root/one/eight.scm
blob: fb944e7d8b99eb1c75d91be63a929e07e954e520 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
;;; Under Creative Commons Attribution-ShareAlike 4.0
;;; International. See
;;; <https://creativecommons.org/licenses/by-sa/4.0/>.

(define-module (one eight)
  #:export (cube double
            div-three improve
            good-enough? cbrt-iter
            cbrt-sicp))

(use-modules (one six))

(define (cube x)
  (* (square x) x))

(define (double x)
  (* 2 x))

(define (div-three x)
  (/ x 3.0))

(define (improve y x)
  (div-three (+ (/ x (square y))
                (double y))))

(define (good-enough? y x)
  (< (abs (- (cube y) x))
     0.001))

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

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