From: Ramakrishnan Muthukrishnan Date: Thu, 24 Jun 2010 02:54:15 +0000 (+0530) Subject: a much clearer solution of 2.20 X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/about/%22news.html/%3C?a=commitdiff_plain;h=d0a480e7afa451f8959d1ed8f8211930b86798a5;p=sicp.git a much clearer solution of 2.20 --- diff --git a/src/sicp/ex2_20.clj b/src/sicp/ex2_20.clj index 5823309..3bab563 100644 --- a/src/sicp/ex2_20.clj +++ b/src/sicp/ex2_20.clj @@ -24,6 +24,20 @@ (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)))) + (deftest test-same-parity (are [x y] [= x y] (same-parity 1 2 3 4 5 6 7) (list 1 3 5 7) @@ -33,3 +47,8 @@ (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)))