From 49a044d75ec5b892019728bdc1d36f766c9d5ff2 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com> Date: Fri, 23 Jul 2010 01:22:19 +0530 Subject: [PATCH] solution to 2.31 --- src/sicp/ex2_31.clj | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/sicp/ex2_31.clj diff --git a/src/sicp/ex2_31.clj b/src/sicp/ex2_31.clj new file mode 100644 index 0000000..e784ca8 --- /dev/null +++ b/src/sicp/ex2_31.clj @@ -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)))) -- 2.45.2