From 623562d251028d8ffc0a5752de2a8b12a739e1bf Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com>
Date: Mon, 21 Jun 2010 19:51:31 +0530
Subject: [PATCH] added another solution and test for 2.20

---
 src/sicp/ex2_20.clj | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/src/sicp/ex2_20.clj b/src/sicp/ex2_20.clj
index 24b5f5e..5823309 100644
--- a/src/sicp/ex2_20.clj
+++ b/src/sicp/ex2_20.clj
@@ -9,8 +9,27 @@
 			    (odd? y)))
 		  xs)))
 
+;; another implementation
+(defn- calc-parity [pred xs result]
+  (if (empty? xs)
+    (reverse result)
+    (let [a (first xs)
+	t (pred a)]
+      (if t
+	(calc-parity pred (rest xs) (cons a result))
+	(calc-parity pred (rest xs) result)))))
+
+(defn same-parity-2 [x & xs]
+  (if (even? x)
+    (cons x (calc-parity even? xs '()))
+    (cons x (calc-parity odd? xs '()))))
+
 (deftest test-same-parity
   (are [x y] [= x y]
        (same-parity 1 2 3 4 5 6 7) (list 1 3 5 7)
        (same-parity 2 3 4 5 6 7) (list 2 4 6)))
 
+(deftest test-same-parity-2
+  (are [x y] [= x y]
+       (same-parity-2 1 2 3 4 5 6 7) (list 1 3 5 7)
+       (same-parity-2 2 3 4 5 6 7) (list 2 4 6)))
-- 
2.45.2