Created
January 17, 2012 20:11
-
-
Save stoitsev/1628616 to your computer and use it in GitHub Desktop.
Scheme streams
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
(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