type 'a stream = Cons of 'a * (unit -> 'a stream)

let rec from v = Cons (v, fun () -> from (v+1))

let rec first n (Cons(h, st)) =
  if n = 0 then [] else h :: first (n-1) (st())

let rec filter f (Cons(h, st)) =
  let ft = fun() -> filter f (st ())
  in if f h then Cons (h, ft) else ft()

let rec sieve (Cons(p, st)) =
  Cons (p, fun() -> sieve (filter (fun n -> n mod p <> 0) (st())))

let primes = sieve (from 2)



This document was generated using caml2html