]> git.rkrishnan.org Git - sicp.git/commitdiff
solution to 2.31
authorRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Thu, 22 Jul 2010 19:52:19 +0000 (01:22 +0530)
committerRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Thu, 22 Jul 2010 19:52:19 +0000 (01:22 +0530)
src/sicp/ex2_31.clj [new file with mode: 0644]

diff --git a/src/sicp/ex2_31.clj b/src/sicp/ex2_31.clj
new file mode 100644 (file)
index 0000000..e784ca8
--- /dev/null
@@ -0,0 +1,16 @@
+(ns sicp.ex2_31
+  (:use [clojure.test]))
+
+(defn tree-map [func tree]
+  (map (fn [sub-tree]
+        (if (seq? sub-tree)
+          (tree-map func sub-tree)
+          (func sub-tree)))
+       tree))
+
+(defn square-tree [tree]
+  (tree-map #(* % %) tree))
+
+(deftest test-tree-map-with-sq
+  (are [x y] [= x y]
+       (square-tree '(1 (2 (3 4) 5) (6 7))) '(1 (4 (9 16) 25) (36 49))))