type 'a list_t =
| Nil
| Cons of 'a * 'a node
and 'a node
= 'a list_t
Lazy.t
let rec lazy_map fn xs = lazy (
| Nil -> Nil
| Cons(x, rest) -> Cons (fn x, lazy_map fn rest)
)
let rec lazy_take n xs = lazy (
match (n,
Lazy.force xs
) with | (0, _) -> Nil
| (_, Nil) -> Nil
| (n, Cons(a, rest)) -> Cons(a, lazy_take (n-1) rest)
)
let rec lazy_repeated x = lazy (
Cons(x, lazy_repeated x)
)
let rec to_list xs =
| Nil -> []
| Cons(a, rest) -> a::to_list rest
let rec lazy_merge xs ys =
match (Lazy.force xs,
Lazy.force ys
) with | (_, Nil) -> xs
| (Nil, _) -> ys
| (Cons(x, xs'), Cons(y, ys')) ->
if x = y then lazy (Cons(x, lazy_merge xs' ys'))
else if x < y then lazy (Cons(x, lazy_merge xs' ys))
else lazy (Cons(y, lazy_merge xs ys'))
let rec lazy_iter fn xs =
| Nil -> ()
| Cons(x, rest) -> fn x; lazy_iter fn rest
let _ =
let rec h = lazy (Cons(1, lazy_merge (lazy_map (( * ) 2) h) (lazy_map (( * ) 3) h))) in
lazy_iter
(fun x
-> ignore x
) (lazy_take
10 h
)
dHlwZSAnYSBsaXN0X3QgPQogIHwgTmlsCiAgfCBDb25zIG9mICdhICogJ2Egbm9kZQphbmQgJ2Egbm9kZSA9ICdhIGxpc3RfdCBMYXp5LnQKCmxldCByZWMgbGF6eV9tYXAgZm4geHMgPSBsYXp5ICgKICBtYXRjaCBMYXp5LmZvcmNlIHhzIHdpdGgKICAgIHwgTmlsIC0+IE5pbAogICAgfCBDb25zKHgsIHJlc3QpIC0+IENvbnMgKGZuIHgsIGxhenlfbWFwIGZuIHJlc3QpCikKCmxldCByZWMgbGF6eV90YWtlIG4geHMgPSBsYXp5ICgKICBtYXRjaCAobiwgTGF6eS5mb3JjZSB4cykgd2l0aAogICAgfCAoMCwgXykgLT4gTmlsCiAgICB8IChfLCBOaWwpIC0+IE5pbAogICAgfCAobiwgQ29ucyhhLCByZXN0KSk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