]> git.rkrishnan.org Git - sicp.git/blob - src/sicp/ex2_27.clj
Solution to 4.33. This had been difficult to get right, though conceptually it was
[sicp.git] / src / sicp / ex2_27.clj
1 (ns sicp.ex2_27
2   (:refer-clojure :exclude (reverse))
3   (:use [clojure.test]))
4
5 (def x (list (list 1 2) (list 3 4)))
6
7 (defn deep-reverse [lst]
8   (cond (not (seq? lst)) lst
9         (empty? lst) nil
10         :else (cons (deep-reverse (last lst)) (deep-reverse (butlast lst)))))
11
12 (deftest test-simple-list
13   (are [x y] [= x y]
14        (deep-reverse '(1 2 3 4))     '(4 3 2 1)
15        (deep-reverse '((1 2) 3 4))   '(4 3 (2 1))
16        (deep-reverse '((1 2) (3 4))) '((4 3) (2 1))))