Last active
September 14, 2017 15:12
-
-
Save irtaylor/f545b422de1661d79247dc8053402e2c to your computer and use it in GitHub Desktop.
Scheme Implementation of the Filter-Map-Reduce pattern
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
; return a new list, where elements that do not | |
; meet the conditions of the filter function are removed | |
(define myfilter | |
(lambda (func lat) | |
(cond | |
((null? lat) (quote ())) | |
((func (car lat)) (cons (car lat) | |
(myfilter func (cdr lat)))) | |
(else | |
(myfilter func (cdr lat)))))) | |
; return a new list, where each element in lat has been | |
; transformed by func | |
(define mymap | |
(lambda (func lat) | |
(cond | |
((null? lat) (quote ())) | |
(else | |
(cons (func (car lat)) | |
(mymap (cdr lat) func)))))) | |
; reduce lat to a single value, accumulated in acc | |
(define myreduce | |
(lambda (func lat acc) | |
(cond | |
((null? lat) acc) | |
(else | |
(func (car lat) | |
(myreduce func (cdr lat) acc)))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment