From: Ramakrishnan Muthukrishnan Date: Tue, 7 Sep 2010 10:56:01 +0000 (+0530) Subject: solution to 2.62 X-Git-Url: https://git.rkrishnan.org/uri/URI:DIR2:%5B%5E?a=commitdiff_plain;h=689f4a611ae436822281a9abbb3b89edbef29802;p=sicp.git solution to 2.62 --- diff --git a/src/sicp/ex2_62.clj b/src/sicp/ex2_62.clj new file mode 100644 index 0000000..0ca7117 --- /dev/null +++ b/src/sicp/ex2_62.clj @@ -0,0 +1,25 @@ +(ns sicp.ex2_62 + (:use [clojure.test])) + +(defn union-set [set1 set2] + (let [x1 (first set1) + x2 (first set2)] + (cond (empty? set1) set2 + (empty? set2) set1 + (= x1 x2) (cons x1 (union-set (rest set1) + (rest set2))) + (< x1 x2) (cons x1 (union-set (rest set1) + set2)) + (> x1 x2) (cons x2 (union-set set1 + (rest set2)))))) + +(deftest test-union + (are [x y] [= x y] + (union-set '() '()) '() + (union-set '(1) '()) '(1) + (union-set '() '(1)) '(1) + (union-set '(1 2 3) '(1 2 3)) '(1 2 3) + (union-set '(1 2 3 4) '(1 2 3)) '(1 2 3 4) + (union-set '(1 2 4) '(3 4 5)) '(1 2 3 4 5) + (union-set '(1 2 3 4) '(5 6 7 8)) '(1 2 3 4 5 6 7 8) + (union-set '(1 2 3) '(3 4 5 6)) '(1 2 3 4 5 6)))