3 [sicp.ch2-2 :only (accumulate)]))
5 ;; Fill in the missing expressions to complete the
6 ;; following definitions of some basic list-manipulation
7 ;; operations as accumulations:
9 ;; (define (map p sequence)
10 ;; (accumulate (lambda (x y) <??>) nil sequence))
11 (defn mymap [p sequence]
12 (accumulate (fn [x y] (cons (p x) y)) nil sequence))
15 (is [= (mymap #(* % %) '(1 2 3))
18 ;; (define (append seq1 seq2)
19 ;; (accumulate cons <??> <??>))
20 (defn append [seq1 seq2]
21 (accumulate cons seq2 seq1))
24 (is [= (append '(1 2 3) '(4 5 6))
27 ;; (define (length sequence)
28 ;; (accumulate <??> 0 sequence))
29 (defn length [sequence]
30 (accumulate (fn [x y] (inc y)) 0 sequence))
34 (length '(1 2 3 4 5)) 5
35 (length '((1 2) 3 4 5) 4)))