From: Ramakrishnan Muthukrishnan Date: Fri, 2 Jul 2010 07:18:44 +0000 (+0530) Subject: starting with section 2.2.2 X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/css/flags//%22%3C?a=commitdiff_plain;h=28a4f724fcc3c9a805e7185ee4a1be24befb4e6a;p=sicp.git starting with section 2.2.2 --- diff --git a/src/sicp/ch2_2.clj b/src/sicp/ch2_2.clj index 7af5040..2fd2f9e 100644 --- a/src/sicp/ch2_2.clj +++ b/src/sicp/ch2_2.clj @@ -59,4 +59,33 @@ (append (rest list1) list2)))) (append one-thru-four one-thru-four) -;;=> (1 2 3 4 1 2 3 4) \ No newline at end of file +;;=> (1 2 3 4 1 2 3 4) + +;; mapping over lists +(defn scale-list [items factor] + (if (empty? items) + nil + (cons (* factor (first items)) + (scale-list (rest items) factor)))) + +(defn map [proc items] + (if (empty? items) + nil + (cons (proc (first items)) + (map proc (rest items))))) + +(defn scale-list-with-map [items factor] + (map (fn [item] (* item factor)) items)) + +;; 2.2.2 +(def x (cons (list 1 2) (list 3 4))) + +(length x) +;;=> 3 + +;; count-leaves +(defn count-leaves [coll] + (cond (nil? coll) 0 + (not (seq? coll)) 1 + :else (+ (count-leaves (first coll)) + (count-leaves (next coll))))) \ No newline at end of file