Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Accumulate
; 2.33
(define (accumulate op initial items) ; accumulate also referred to as "reduce"
(if (null? items)
initial
(op (car items) (accumulate op initial (cdr items)))))
; Defining collection operations in terms of accumulate
(define (append seq1 seq2)
(accumulate cons seq2 seq1))
(define (map proc items)
(accumulate (lambda (x y) (cons (proc x) y)) '() items))
(define (filter proc items)
(accumulate (lambda (x y) (if (proc x) (cons x y) y)) '() items))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.