From 85b3517551d15875d78536215e5259032ed49133 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com> Date: Thu, 22 Jul 2010 09:13:05 +0530 Subject: [PATCH] another solution to 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 3bab563..079248f 100644 --- a/src/sicp/ex2_20.clj +++ b/src/sicp/ex2_20.clj @@ -38,6 +38,15 @@ (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) @@ -52,3 +61,13 @@ (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))) -- 2.45.2