type 'a t = Cons of 'a option * ('a t Lazy.t)
let rec uniform elt = Cons (elt, lazy (uniform elt))
let is_empty = function
| Cons (None,_) -> true
| _ -> false
let read = function
| Cons (Some thing,next) -> (thing, Lazy.force next)
| _ -> failwith "End of stream"
let rec map f = function
| Cons (None,next) -> Cons (None, lazy (map f (Lazy.force next)))
| Cons (Some thing, next) -> Cons (Some (f thing), lazy (map f (Lazy.force next)))
let rec flatten = function
| Cons (Some (head::tail),stream) -> Cons(Some head, lazy (flatten (Cons (Some tail,stream))))
| Cons (Some ([]),stream) -> flatten (Lazy.force stream)
| Cons (None,stream) -> uniform None
let rec of_in_channel chan =
try
Cons (Some (input_char chan), lazy (of_in_channel chan))
with
| _ -> uniform None