From aa6eaa6ab06560d043d30f9c0f5fd823afa6b56c Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com> Date: Fri, 23 Jul 2010 01:22:14 +0530 Subject: [PATCH] solution to 2.30 --- src/sicp/ex2_30.clj | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/sicp/ex2_30.clj diff --git a/src/sicp/ex2_30.clj b/src/sicp/ex2_30.clj new file mode 100644 index 0000000..e79f06b --- /dev/null +++ b/src/sicp/ex2_30.clj @@ -0,0 +1,25 @@ +(ns sicp.ex2_30 + (:use [clojure.test])) + +;; without map +(defn square-tree [tree] + (cond (nil? tree) nil + (not (seq? tree)) (* tree tree) + :else (cons (square-tree (first tree)) + (square-tree (next tree))))) + +(deftest test-sq-tree + (are [x y] [= x y] + (square-tree '(1 (2 (3 4) 5) (6 7))) '(1 (4 (9 16) 25) (36 49)))) + +;; with map +(defn square-tree-with-map [tree] + (map (fn [sub-tree] + (if (seq? sub-tree) + (square-tree-with-map sub-tree) + (* sub-tree sub-tree))) + tree)) + +(deftest test-sq-tree-map + (are [x y] [= x y] + (square-tree-with-map '(1 (2 (3 4) 5) (6 7))) '(1 (4 (9 16) 25) (36 49)))) -- 2.45.2