]> git.rkrishnan.org Git - sicp.git/commitdiff
solution to 1.46
authorRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Sun, 30 May 2010 03:06:07 +0000 (08:36 +0530)
committerRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Sun, 30 May 2010 03:06:07 +0000 (08:36 +0530)
src/sicp/ex1_46.clj [new file with mode: 0644]

diff --git a/src/sicp/ex1_46.clj b/src/sicp/ex1_46.clj
new file mode 100644 (file)
index 0000000..0cf3d0c
--- /dev/null
@@ -0,0 +1,33 @@
+(ns sicp.ex1_46
+  (:use [clojure.contrib test-is]
+       [sicp utils]))
+
+(defn iterative-improve [good-enough-fn? improve-guess-fn]
+  (fn [guess]
+    (let [new-guess (improve-guess-fn guess)]
+      (if (good-enough-fn? guess new-guess)
+       new-guess
+       (recur new-guess)))))
+
+(defn sqrt [x]
+  (let [initial-guess 1.0]
+    ((iterative-improve
+      (fn [old new] (< (Math/abs (- old new)) 0.00001))
+      (fn [guess] (average guess x)))
+     initial-guess)))
+
+(defn fixed-point [f initial-guess]
+  ((iterative-improve
+    (fn [old new] (< (Math/abs (- old new)) 0.00001))
+    f) initial-guess))
+
+(comment
+(sqrt 4)
+;;=> 3.9999942779541016
+user> (sqrt (* 2 2))
+;;=> 3.9999942779541016
+user> (fixed-point #(Math/cos %) 1.0)
+;;=> 0.7390822985224024
+user> (fixed-point #(+ (Math/cos %) (Math/sin %)) 1.0)
+;;=> 1.2587315962971173  
+)
\ No newline at end of file