Skip to content

Instantly share code, notes, and snippets.

@PierreBdR
Last active August 29, 2015 14:08
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 PierreBdR/b37e5f17073f72370fc4 to your computer and use it in GitHub Desktop.
Save PierreBdR/b37e5f17073f72370fc4 to your computer and use it in GitHub Desktop.
(define (interleave-streams . ss)
(define (helper to-process processed)
(λ ()
(if (null? to-process)
((helper (reverse processed) null))
(let* ([next (car to-process)]
[pr (next)])
(cons (car pr) (helper (cdr to-process) (cons (cdr pr) processed)))))))
(helper ss null))
; Take a non-empty list and return a cyclic mlist
(define (list->cyclic-mlist lst)
(define (helper l)
(if (null? l)
#f
(let ([next (helper (cdr l))])
(if next
(cons (mcons (car l) (car next)) (cdr next))
(let ([val (mcons (car l) null)])
(cons val val))))))
(let ([pr (helper lst)])
(begin
(set-mcdr! (cdr pr) (car pr))
(car pr))))
; Interleave n streams together
(define (interleave-streams2 . ss)
(define (helper to-process)
(thunk
(let ([pr ((mcar to-process))])
(begin
(set-mcar! to-process (cdr pr))
(cons (car pr) (helper (mcdr to-process)))))))
(helper (list->cyclic-mlist ss)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment