From a574e2a7e1bf084315054a1e7e4cdb48d60dcb05 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com>
Date: Tue, 4 May 2010 16:26:04 +0530
Subject: [PATCH] cleaner implementation making use of sum

---
 src/sicp/ex1_29.clj | 33 ++++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/src/sicp/ex1_29.clj b/src/sicp/ex1_29.clj
index cf30a23..0f36ae5 100644
--- a/src/sicp/ex1_29.clj
+++ b/src/sicp/ex1_29.clj
@@ -3,20 +3,23 @@
 	[clojure.contrib trace test-is]))
 
 ;; simpson's rule of integration
-(defn make-fac [k n]
-  (cond (= k 0) 1
-	(= k n) 1
-	(even? k) 2
-	:else 4))
-
-(defn simpson-sum [term a next-fn b n k]
-  (let [fac (make-fac k n)]
-    (if (> k n)
-      0
-      (+ (* fac (term a))
-	 (simpson-sum term (next-fn a) next-fn b n (+ k 1))))))
+(defn sum [term a next b]
+  (if (> a b)
+    0
+    (+ (term a)
+       (sum term (next a) next b))))
 
 (defn simpson-rule [f a b n]
-  (let [h (/ (- b a) n)]
-    (* (simpson-sum f a #(+ h %) b n 0)
-       (/ h 3.0))))
+  (let [h (/ (- b a) n)
+	make-fac (fn [k]
+		   (cond (= k 0) 1
+			 (= k n) 1
+			 (even? k) 2
+			 :else 4))
+	simp-term (fn [k]
+		    (* (make-fac k)
+		       (f (+ a (* k h)))))]
+    (* (/ h 3.0)
+       (sum simp-term 0 inc n))))
+
+;;; lots of great ideas in the above exercise.
\ No newline at end of file
-- 
2.45.2