4 ;; clojure has a slightly different notation compared to Scheme for the
5 ;; arbitraty number of argument notation.
6 (defn same-parity [x & xs]
7 (cons x (filter (fn [y] (if (even? x)
12 ;; another implementation
13 (defn- calc-parity [pred xs result]
19 (calc-parity pred (rest xs) (cons a result))
20 (calc-parity pred (rest xs) result)))))
22 (defn same-parity-2 [x & xs]
24 (cons x (calc-parity even? xs '()))
25 (cons x (calc-parity odd? xs '()))))
27 (defn- calc-parity-3 [pred xs]
33 (cons a (calc-parity-3 pred (rest xs)))
34 (calc-parity-3 pred (rest xs))))))
36 (defn same-parity-3 [x & xs]
38 (cons x (calc-parity-3 even? xs))
39 (cons x (calc-parity-3 odd? xs))))
41 (defn same-parity-4 [x & xs]
42 (conj (filter #(if (even? x) (even? %) (odd? %)) xs) x))
44 ;; result of the discussion in bang-fp
45 (defn same-parity-5 [x & xs]
46 (cond (nil? xs) (list x)
47 (= (even? x) (even? (first xs))) (cons x (apply same-parity-5 xs))
48 :else (apply same-parity-5 (cons x (rest xs)))))
50 (deftest test-same-parity
52 (same-parity 1 2 3 4 5 6 7) (list 1 3 5 7)
53 (same-parity 2 3 4 5 6 7) (list 2 4 6)))
55 (deftest test-same-parity-2
57 (same-parity-2 1 2 3 4 5 6 7) (list 1 3 5 7)
58 (same-parity-2 2 3 4 5 6 7) (list 2 4 6)))
60 (deftest test-same-parity-3
62 (same-parity-3 1 2 3 4 5 6 7) (list 1 3 5 7)
63 (same-parity-3 2 3 4 5 6 7) (list 2 4 6)))
65 (deftest test-same-parity-4
67 (same-parity-4 1 2 3 4 5 6 7) (list 1 3 5 7)
68 (same-parity-4 2 3 4 5 6 7) (list 2 4 6)))
70 (deftest test-same-parity-5
72 (same-parity-5 1 2 3 4 5 6 7) (list 1 3 5 7)
73 (same-parity-5 2 3 4 5 6 7) (list 2 4 6)))