From: Ramakrishnan Muthukrishnan Date: Thu, 22 Jul 2010 19:51:50 +0000 (+0530) Subject: text examples upto 2.2.3 X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/priv/vdrive/global?a=commitdiff_plain;h=542fc334464dfade8fe5517b8756f67b82679c3e;p=sicp.git text examples upto 2.2.3 --- 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