From: Ramakrishnan Muthukrishnan Date: Wed, 1 Sep 2010 19:54:06 +0000 (+0530) Subject: solution to 2.54 X-Git-Url: https://git.rkrishnan.org/%5B/frontends/%22file:/nxhtml.html?a=commitdiff_plain;h=a0fa6d5035f52ed146bbe2a81da9d6e040be47d6;p=sicp.git solution to 2.54 --- diff --git a/src/sicp/ex2_54.clj b/src/sicp/ex2_54.clj new file mode 100644 index 0000000..0039c29 --- /dev/null +++ b/src/sicp/ex2_54.clj @@ -0,0 +1,27 @@ +(ns sicp.ex2_54 + (:use [clojure.test])) + +(defn atom? [x] + (not (list? x))) + +(defn eq? [x y] + (if (not (atom? x)) + false + (= x y))) + +(defn equal? [a b] + (cond (empty? a) (empty? b) + (atom? (first a)) + (if (eq? (first a) (first b)) + (equal? (rest a) (rest b)) + false) + :else (and (equal? (first a) (first b)) + (equal? (rest a) (rest b))))) + +(deftest test-equality + (are [x y] [= x y] + (equal? '(1) '(1)) true + (equal? '(1) '(2)) false + (equal? '(1 2) '(1 2)) true + (equal? '(1 2 (3)) '(1 2 (3))) true + (equal? '(1 2 3) '(1 2 (3))) false)) \ No newline at end of file