Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@stoitsev
Created January 17, 2012 20:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoitsev/1628616 to your computer and use it in GitHub Desktop.
Save stoitsev/1628616 to your computer and use it in GitHub Desktop.
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