]> git.rkrishnan.org Git - sicp.git/blobdiff - src/sicp/ex2_20.clj
Merge branch 'master' of github.com:vu3rdd/sicp
[sicp.git] / src / sicp / ex2_20.clj
index 582330968a957aaeba1b5bcb7f19e024a64943a6..590ff31b8c93a3bf289c95a97a43df14e4926027 100644 (file)
@@ -14,7 +14,7 @@
   (if (empty? xs)
     (reverse result)
     (let [a (first xs)
-       t (pred a)]
+          t (pred a)]
       (if t
        (calc-parity pred (rest xs) (cons a result))
        (calc-parity pred (rest xs) result)))))
     (cons x (calc-parity even? xs '()))
     (cons x (calc-parity odd? xs '()))))
 
+(defn- calc-parity-3 [pred xs]
+  (if (empty? xs)
+    nil
+    (let [a (first xs)
+          t (pred a)]
+      (if t
+       (cons a (calc-parity-3 pred (rest xs)))
+       (calc-parity-3 pred (rest xs))))))
+
+(defn same-parity-3 [x & xs]
+  (if (even? x)
+    (cons x (calc-parity-3 even? xs))
+    (cons x (calc-parity-3 odd? xs))))
+
+(defn same-parity-4 [x & xs]
+  (conj (filter #(if (even? x) (even? %) (odd? %)) xs) x))
+
+;; result of the discussion in bang-fp
+(defn same-parity-5 [x & xs]
+  (cond (nil? xs) (list x)
+       (= (even? x) (even? (first xs))) (cons x (apply same-parity-5 xs))
+       :else (apply same-parity-5 (cons x (rest xs)))))
+
 (deftest test-same-parity
   (are [x y] [= x y]
        (same-parity 1 2 3 4 5 6 7) (list 1 3 5 7)
   (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)))
+
+(deftest test-same-parity-3
+  (are [x y] [= x y]
+       (same-parity-3 1 2 3 4 5 6 7) (list 1 3 5 7)
+       (same-parity-3 2 3 4 5 6 7) (list 2 4 6)))
+
+(deftest test-same-parity-4
+  (are [x y] [= x y]
+       (same-parity-4 1 2 3 4 5 6 7) (list 1 3 5 7)
+       (same-parity-4 2 3 4 5 6 7) (list 2 4 6)))
+
+(deftest test-same-parity-5
+  (are [x y] [= x y]
+       (same-parity-5 1 2 3 4 5 6 7) (list 1 3 5 7)
+       (same-parity-5 2 3 4 5 6 7) (list 2 4 6)))