Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scheme streams
(define (delay x)
(lambda () x)
)
(define (force x)
(x)
)
(define (cons-stream x y)
(cons x (delay y))
)
(define (head stream)
(car stream)
)
(define (tail stream)
(force (cdr stream))
)
(define empty-stream '())
(define empty-stream? null?)
(define (append-streams str1 str2)
(if (empty-stream? s1)s2
(cons-stream (head str1) (append-stream (tail str1)str2))
)
)
(define (map-streams proc stream)
(if(empty-stram? stream)the-empty-stream
(cons-stream (proc (head stream)(map-stream proc (tail stream))))
)
)
(define (filter-stream pred? stream)
(cond ((empty-stream? stream) the-empty-stream)
((pred? (head stream))
(cons-stream (head stream)(filter-stream pred? (tail stream))))
(else (filter-stream pred? (tail stream)))
)
)
(define (add-streams str1 str2)
(cons-stream (+ (head str1 )(head str2))
(add-stream (tail str1)
(tail str2))
)
)
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.