Skip to content

Instantly share code, notes, and snippets.

@jaked
Created November 16, 2010 06:39
Show Gist options
  • Save jaked/701532 to your computer and use it in GitHub Desktop.
Save jaked/701532 to your computer and use it in GitHub Desktop.
type 'a stream = S of 'a * 'a stream Lazy.t
let rec map f s =
match s with
| lazy (S (h, t)) -> S (f h, lazy (map f t))
let rec nth s n =
match s, n with
| lazy (S (h, _)), 0 -> h
| lazy (S (_, t)), n -> nth t (n - 1)
let rec merge t2 t3 t5 =
match t2, t3, t5 with
| lazy (S (h2, t2')), lazy (S (h3, t3')), lazy (S (h5, t5')) ->
let h = min h2 (min h3 h5) in
let t =
lazy
(merge
(if h2 = h then t2' else t2)
(if h3 = h then t3' else t3)
(if h5 = h then t5' else t5)) in
S (h, t)
| _ -> assert false
let rec hamming = lazy (S (1, lazy (merge t2 t3 t5)))
and t2 = lazy (map (fun k -> k * 2) hamming)
and t3 = lazy (map (fun k -> k * 3) hamming)
and t5 = lazy (map (fun k -> k * 5) hamming)
;;
nth hamming 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment