From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com>
Date: Sat, 27 Mar 2010 19:21:31 +0000 (+0530)
Subject: rearranging the procedures and adding the execution examples under (comment ..)
X-Git-Url: https://git.rkrishnan.org/%5B/%5D%20/file/URI:LIT:krugkidfnzsc4/@@named=/frontends/webapi.txt?a=commitdiff_plain;h=81eb83eeb81453e01c6aaef20e5523d56391925c;p=sicp.git

rearranging the procedures and adding the execution examples under (comment ..)
---

diff --git a/chapter1/ch1_1.clj b/chapter1/ch1_1.clj
index d5eb731..b96191b 100644
--- a/chapter1/ch1_1.clj
+++ b/chapter1/ch1_1.clj
@@ -96,21 +96,21 @@
   )
 
 ;; 1.1.7 Square root finding using Newton's method
-(defn sqrt-iter [guess x]
-  (if (good-enough? guess x)
-    guess
-    (sqrt-iter (improve guess x)
-	       x)))
+(defn average [x y]
+  (/ (+ x y) 2))
 
 (defn improve [guess x]
   (average guess (/ x guess)))
 
-(defn average [x y]
-  (/ (+ x y) 2))
-
 (defn good-enough? [guess x]
   (< (abs (- (square guess) x)) 0.001))
 
+(defn sqrt-iter [guess x]
+  (if (good-enough? guess x)
+    guess
+    (sqrt-iter (improve guess x)
+	       x)))
+
 (defn sqrt [x]
   (sqrt-iter 1.0 x))
 
@@ -146,49 +146,50 @@
 ;; limited precision. This makes our test inadequate for very large numbers.
 ;; Explain these statements, with examples showing how the test fails for small
 ;; and large numbers.
-user> (sqrt (square 0.001))
-0.031260655525445276
-user> (sqrt (square 0.2))
-0.20060990407779591
-user> (sqrt (square 0.01))
-0.03230844833048122
-user> (sqrt (square 0.02))
-0.0354008825558513
-user> (sqrt (square 10))
-10.000000000139897
-user> (sqrt (square 100))
-100.00000025490743
-user> (sqrt (square 200))
-200.000000510076
-user> (sqrt (square 2))
-2.0000000929222947
-user> (sqrt (square 0.1))
-0.10032578510960607
-user> (sqrt (square 0.01))
-0.03230844833048122
-user> (sqrt (square 10000))
-10000.0
-user> (sqrt (square 20000))
-20000.0
-user> (sqrt (square 200000))
-200000.0
-user> (sqrt (square 20000000))
-2.0E7
-user> (sqrt (square 20000000000))
-2.0E10
-user> (sqrt (square 200000.012))
-200000.012
-user> (sqrt (square 2000000.123))
-2000000.123
-user> (sqrt (square 200000000.123))
-2.00000000123E8
-user> (sqrt (square 2000000000.123))
-2.000000000123E9
-user> (sqrt (square 20000000000.123))
-2.0000000000123E10
-user> (sqrt (square 2000000000000.123))
-2.000000000000123E12
-
+(comment
+ user> (sqrt (square 0.001))
+ 0.031260655525445276
+ user> (sqrt (square 0.2))
+ 0.20060990407779591
+ user> (sqrt (square 0.01))
+ 0.03230844833048122
+ user> (sqrt (square 0.02))
+ 0.0354008825558513
+ user> (sqrt (square 10))
+ 10.000000000139897
+ user> (sqrt (square 100))
+ 100.00000025490743
+ user> (sqrt (square 200))
+ 200.000000510076
+ user> (sqrt (square 2))
+ 2.0000000929222947
+ user> (sqrt (square 0.1))
+ 0.10032578510960607
+ user> (sqrt (square 0.01))
+ 0.03230844833048122
+ user> (sqrt (square 10000))
+ 10000.0
+ user> (sqrt (square 20000))
+ 20000.0
+ user> (sqrt (square 200000))
+ 200000.0
+ user> (sqrt (square 20000000))
+ 2.0E7
+ user> (sqrt (square 20000000000))
+ 2.0E10
+ user> (sqrt (square 200000.012))
+ 200000.012
+ user> (sqrt (square 2000000.123))
+ 2000000.123
+ user> (sqrt (square 200000000.123))
+ 2.00000000123E8
+ user> (sqrt (square 2000000000.123))
+ 2.000000000123E9
+ user> (sqrt (square 20000000000.123))
+ 2.0000000000123E10
+ user> (sqrt (square 2000000000000.123))
+ 2.000000000000123E12
+ )
 ;; An alternative strategy for implementing good-enough? is to watch how guess
 ;; changes from one iteration to the next and to stop when the change is a very
 ;; small fraction of the guess.
@@ -209,7 +210,7 @@ user> (sqrt (square 2000000000000.123))
 
 (defn sqrt [x]
   (sqrt-iter x 1.0 x))
-
+(comment
 user> (sqrt (square 0.01))
 0.010000000025490743
 user> (sqrt (square 0.001))
@@ -234,7 +235,7 @@ user> (sqrt 9)
 3.000000001396984
 user> (sqrt 81)
 9.000000000007091
-
+)
 ;; exercise 1.8: cube root
 (defn cube [x]
   (* x x x))
@@ -251,6 +252,7 @@ user> (sqrt 81)
 (defn cuberoot [x]
   (cubert-iter x 1.0 x))
 
+(comment
 user> (cuberoot (cube 2))
 2.000000000012062
 user> (cuberoot (cube 10))
@@ -262,3 +264,23 @@ user> (cuberoot (cube 0.001))
 user> (cuberoot (cube 0.0001))
 1.000000000000001E-4
 user>
+)
+;; section 1.1.8
+;; hiding the non-public procedure definitions
+(defn- sqrt-iter [guess x]
+  (if (good-enough? guess x)
+    guess
+    (sqrt-iter (improve guess x)
+	       x)))
+
+(defn- improve [guess x]
+  (average guess (/ x guess)))
+
+(defn- average [x y]
+  (/ (+ x y) 2))
+
+(defn- good-enough? [guess x]
+  (< (abs (- (square guess) x)) 0.001))
+
+(defn sqrt [x]
+  (sqrt-iter 1.0 x))