Created
October 30, 2019 15:21
-
-
Save chelseatroy/5488e61ca4e9b6195801e8b113defc74 to your computer and use it in GitHub Desktop.
Accumulate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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