Skip to content

Instantly share code, notes, and snippets.

@orb
Created February 23, 2013 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orb/5018630 to your computer and use it in GitHub Desktop.
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…
(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