]> git.rkrishnan.org Git - sicp.git/blob - src/sicp/ex2_54.clj
Solution to 4.33. This had been difficult to get right, though conceptually it was
[sicp.git] / src / sicp / ex2_54.clj
1 (ns sicp.ex2_54
2   (:use [clojure.test]))
3
4 (defn atom? [x]
5   (not (list? x)))
6
7 (defn eq? [x y]
8   (if (not (atom? x))
9     false
10     (= x y)))
11
12 (defn equal? [a b]
13   (cond (and (atom? a) (atom? b)) (eq? a b)
14         (and (not (atom? a)) (not (atom? b))) (and (empty? a) (empty? b)) 
15         (atom? (first a))
16         (if (eq? (first a) (first b))
17           (equal? (rest a) (rest b))
18           false)
19         :else (and (equal? (first a) (first b))
20                    (equal? (rest a) (rest b)))))
21
22 (deftest test-equality
23   (are [x y] [= x y]
24        (equal? '(1) '(1)) true
25        (equal? '(1) '(2)) false
26        (equal? '(1 2) '(1 2)) true
27        (equal? '(1 2 (3)) '(1 2 (3))) true
28        (equal? '(1 2 3) '(1 2 (3))) false))