Created
February 23, 2013 05:54
-
-
Save orb/5018630 to your computer and use it in GitHub Desktop.
My version of stream-maker from PL class. This function works under the assumption that there is some state that is passed along from thunk to thunk as context. The current stream output value can be computed using val-fn. The state to pass to the next thunk is computed using the next-state function. I think it's much easier to understand, thoug…
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 (my-stream-maker val-fn next-state seed) | |
(define (stream-thunk current-state) | |
(lambda () (cons (val-fn current-state) | |
(stream-thunk (next-state current-state))))) | |
(stream-thunk seed)) | |
(define ones | |
(my-stream-maker identity | |
identity | |
1)) | |
(define (increment n) (+ n 1)) | |
(define nats | |
(my-stream-maker identity | |
increment | |
1)) | |
(define powers-of-two | |
(my-stream-maker (lambda (n) (expt 2 n)) | |
increment | |
0)) | |
(define powers-of-two-another-way | |
(my-stream-maker identity | |
(lambda (n) (* n 2)) | |
1)) | |
(define (rotate-text-left text) | |
(string-append (substring text 1) (substring text 0 1))) | |
(define marquee | |
(my-stream-maker identity | |
rotate-text-left | |
"Hello, World!")) | |
(define (random-increment-or-decrement n) | |
(if (= 0 (random 2)) (+ n 1) (- n 1))) | |
(define random-walk | |
(my-stream-maker identity | |
random-increment-or-decrement | |
0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment