From 28a4f724fcc3c9a805e7185ee4a1be24befb4e6a Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Fri, 2 Jul 2010 12:48:44 +0530 Subject: [PATCH] starting with section 2.2.2 --- src/sicp/ch2_2.clj | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) 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 -- 2.45.2