Skip to content

Instantly share code, notes, and snippets.

@joshpetit
Last active October 6, 2021 13:45
Show Gist options
  • Save joshpetit/34ed9d33707ad7de960c079cb0f320dd to your computer and use it in GitHub Desktop.
Save joshpetit/34ed9d33707ad7de960c079cb0f320dd to your computer and use it in GitHub Desktop.
Scheme functions
(define filters
(lambda ((pred <function>) (stream <stream>))
(cond ((stream-empty? stream) stream)
((pred (stream-first stream))
(stream-cons (stream-first stream)
(filters pred (stream-rest stream))))
(else (filters pred (stream-rest stream))))))
; maps thorugh stream
(define maps
(lambda ((f <function>) (stream <stream>))
(if (stream-empty? stream)
stream
(stream-cons (f (stream-first stream)) (maps f (stream-rest stream))))))
(define reduce
(lambda (base combiner lst) ;base <object> combiner<function> lst<list>
(if (null? lst)
base
(combiner (head lst)
(reduce base combiner (tail lst))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment