From 542fc334464dfade8fe5517b8756f67b82679c3e Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com> Date: Fri, 23 Jul 2010 01:21:50 +0530 Subject: [PATCH] text examples upto 2.2.3 --- src/sicp/ch2_2.clj | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/sicp/ch2_2.clj b/src/sicp/ch2_2.clj index 2fd2f9e..e0107f5 100644 --- a/src/sicp/ch2_2.clj +++ b/src/sicp/ch2_2.clj @@ -1,4 +1,5 @@ -(ns sicp.ch2_2) +(ns sicp.ch2_2 + (:refer-clojure :exclude (map))) (cons 1 (cons 2 @@ -88,4 +89,19 @@ (cond (nil? coll) 0 (not (seq? coll)) 1 :else (+ (count-leaves (first coll)) - (count-leaves (next coll))))) \ No newline at end of file + (count-leaves (next coll))))) + +;; mapping over trees +(defn scale-tree [tree factor] + (cond (nil? tree) nil + (not (seq? tree)) (* tree factor) + :else (cons (scale-tree (first tree) factor) + (scale-tree (next tree) factor)))) + +;; using map +(defn scale-tree-with-map [tree factor] + (map (fn [sub-tree] + (if (seq? sub-tree) + (scale-tree-with-map sub-tree factor) + (* sub-tree factor))) + tree)) \ No newline at end of file -- 2.45.2