From aea8bc4efcc679d331d5bc5c763667a9a89c095e Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com> Date: Tue, 3 Aug 2010 00:11:59 +0530 Subject: [PATCH] Solution to 2.33 --- src/sicp/ex2_33.clj | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/sicp/ex2_33.clj 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))) + -- 2.45.2