It is instructive to note that accumulate is doing recursion on lists (sequences). The operator that combines data ("op") is receiving as its two inputs the first element of the list (car sequence) and the accumulation of the rest of the list. It's the same old list recursion packaged in a more abstract format.
(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence))))) ; 2.33 (define (square x) (* x x)) ; (map2 square '(1 2 3 4)) (define (map2 p sequence) (accumulate (lambda (x y) (cons (p x) y)) '() sequence)) (define (append2 seq1 seq2) (accumulate cons seq2 seq1)) (define (length2 sequence) (accumulate (lambda (x y) (+ 1 y)) 0 sequence)) ; 2.35 (define t (cons (list 1 2) (list 3 4))) ; Domain: A tree (defined as a list with sublists; an atom is a leaf) ; Range: A non-negative integer ; ; Accumulation works as follows: ; * If we have a leaf, then return 1 ; * If we have a tree, then count-leaves ; * Sum the number of leaves by accumulate by means of addition, and the additive id = 0 (define (count-leaves t) (let ((tree? list?)) ; makes the tree abstraction feel more real (accumulate + 0 (map (lambda (subtree) (if (not (tree? subtree)) 1 (count-leaves subtree))) t)))