summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace/sicp/one/eight.scm
diff options
context:
space:
mode:
Diffstat (limited to 'net/ricketyspace/sicp/one/eight.scm')
-rw-r--r--net/ricketyspace/sicp/one/eight.scm38
1 files changed, 38 insertions, 0 deletions
diff --git a/net/ricketyspace/sicp/one/eight.scm b/net/ricketyspace/sicp/one/eight.scm
new file mode 100644
index 0000000..8157c23
--- /dev/null
+++ b/net/ricketyspace/sicp/one/eight.scm
@@ -0,0 +1,38 @@
+;;; Under Creative Commons Attribution-ShareAlike 4.0
+;;; International. See
+;;; <https://creativecommons.org/licenses/by-sa/4.0/>.
+
+(define-module (net ricketyspace sicp 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))
+