Last active
August 29, 2015 14:08
-
-
Save mrfabbri/7b3dbc506c7ba47cbea7 to your computer and use it in GitHub Desktop.
Interleaving streams in Racket
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
#lang racket | |
;; produce a new stream with the elements of the given streams interleaved | |
(define (streams-interleave . streams) | |
(letrec ([loop (lambda (streams) | |
(let ([next ((car streams))]) | |
(cons (car next) | |
(thunk (loop (append (cdr streams) (list (cdr next))))))))]) | |
(thunk (loop streams)))) | |
;; using interleave on some streams | |
(define (ones) (cons 1 ones)) | |
(define (twos) (cons 2 twos)) | |
(define (threes) (cons 3 threes)) | |
(define interleaved (streams-interleave ones twos threes)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Pierre! Good catch, corrected ;-)