summaryrefslogtreecommitdiffstats
path: root/one
diff options
context:
space:
mode:
authorrsiddharth <rsiddharth@ninthfloor.org>2016-03-12 11:37:03 -0500
committerrsiddharth <rsiddharth@ninthfloor.org>2016-03-12 11:37:03 -0500
commit94758d69d5ef1eccf39842fb806e736b31387fa6 (patch)
treea2a509c4b41089daed52c88a4112ddc4f3f45d03 /one
parent1375cccf75aee17bbe576a744583ed9e7f7d939e (diff)
Tried answering 1.1.6
Diffstat (limited to 'one')
-rw-r--r--one/six.scm36
1 files changed, 36 insertions, 0 deletions
diff --git a/one/six.scm b/one/six.scm
new file mode 100644
index 0000000..ab58664
--- /dev/null
+++ b/one/six.scm
@@ -0,0 +1,36 @@
+(define-module (one six)
+ #:export (sqrt-sicp sqrt-nif square good-enough? new-if improve))
+
+(define (square x)
+ (* x x))
+
+(define (good-enough? guess x)
+ (< (abs (- (square guess) x)) 0.001))
+
+(define (average x y)
+ (/ (+ x y) 2))
+
+(define (improve guess x)
+ (average guess (/ x guess)))
+
+(define (new-if predicate then-clause else-clause)
+ (cond (predicate then-clause)
+ (else else-clause)))
+
+(define (sqrt-iter guess x)
+ (if (good-enough? guess x)
+ guess
+ (sqrt-iter (improve guess x)
+ x)))
+
+(define (sqrt-iter-nif guess x)
+ (new-if (good-enough? guess x)
+ guess
+ (sqrt-iter-nif (improve guess x)
+ x)))
+
+(define (sqrt-sicp x)
+ (sqrt-iter 1.0 x))
+
+(define (sqrt-nif x)
+ (sqrt-iter-nif 1.0 x))