From: Ramakrishnan Muthukrishnan Date: Mon, 2 Aug 2010 18:41:59 +0000 (+0530) Subject: Solution to 2.33 X-Git-Url: https://git.rkrishnan.org/somewhere?a=commitdiff_plain;h=aea8bc4efcc679d331d5bc5c763667a9a89c095e;p=sicp.git Solution to 2.33 --- diff --git a/src/sicp/ex2_33.clj b/src/sicp/ex2_33.clj new file mode 100644 index 0000000..57fefcc --- /dev/null +++ b/src/sicp/ex2_33.clj @@ -0,0 +1,36 @@ +(ns sicp.ex2_33 + (:use [clojure.test] + [sicp.ch2-2 :only (accumulate)])) + +;; Fill in the missing expressions to complete the +;; following definitions of some basic list-manipulation +;; operations as accumulations: + +;; (define (map p sequence) +;; (accumulate (lambda (x y) ) nil sequence)) +(defn mymap [p sequence] + (accumulate (fn [x y] (cons (p x) y)) nil sequence)) + +(deftest test-mymap + (is [= (mymap #(* % %) '(1 2 3)) + '(1 4 9)])) + +;; (define (append seq1 seq2) +;; (accumulate cons )) +(defn append [seq1 seq2] + (accumulate cons seq2 seq1)) + +(deftest test-append + (is [= (append '(1 2 3) '(4 5 6)) + '(1 2 3 4 5 6)])) + +;; (define (length sequence) +;; (accumulate 0 sequence)) +(defn length [sequence] + (accumulate (fn [x y] (inc y)) 0 sequence)) + +(deftest test-length + (are [x y] [= x y] + (length '(1 2 3 4 5)) 5 + (length '((1 2) 3 4 5) 4))) +