From 689f4a611ae436822281a9abbb3b89edbef29802 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Tue, 7 Sep 2010 16:26:01 +0530 Subject: [PATCH] solution to 2.62 --- src/sicp/ex2_62.clj | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/sicp/ex2_62.clj 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))) -- 2.45.2