From: Ramakrishnan Muthukrishnan Date: Thu, 22 Jul 2010 19:52:19 +0000 (+0530) Subject: solution to 2.31 X-Git-Url: https://git.rkrishnan.org/simplejson/components/cyclelanguage?a=commitdiff_plain;h=49a044d75ec5b892019728bdc1d36f766c9d5ff2;p=sicp.git solution to 2.31 --- 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))))