From 62c166f66e8f3470977c9a8a680eb6e2ce73a839 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Tue, 3 Aug 2010 16:38:40 +0530 Subject: [PATCH] Solution to 2.35. Yet another enlightening problem --- src/sicp/ex2_35.clj | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/sicp/ex2_35.clj diff --git a/src/sicp/ex2_35.clj b/src/sicp/ex2_35.clj new file mode 100644 index 0000000..0bf7f08 --- /dev/null +++ b/src/sicp/ex2_35.clj @@ -0,0 +1,21 @@ +(ns sicp.ex2_35 + (:use [clojure.test] + [sicp [ch2-2 :only (accumulate)]])) + +;; Redefine count-leaves from section 2.2.2 as an accumulation: + +;; (define (count-leaves t) +;; (accumulate (map ))) +(defn count-leaves-with-accumulate [t] + (accumulate + 0 (map + (fn [x] (if (seq? x) + (count-leaves-with-accumulate x) + 1)) + t))) + +(deftest test-count-leaves + (let [foo (list (list 1 2) 3 4)] + (are [x y] [= x y] + (count-leaves-with-accumulate foo) 4 + (count-leaves-with-accumulate (list foo)) 4 + (count-leaves-with-accumulate (list foo foo)) 8))) \ No newline at end of file -- 2.45.2