Skip to content

Instantly share code, notes, and snippets.

@irtaylor
Last active September 14, 2017 15:12
Show Gist options
  • Save irtaylor/f545b422de1661d79247dc8053402e2c to your computer and use it in GitHub Desktop.
Save irtaylor/f545b422de1661d79247dc8053402e2c to your computer and use it in GitHub Desktop.
Scheme Implementation of the Filter-Map-Reduce pattern
; 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