Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] A (much less) frustrated beginner
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-12-24 (08:59)
From: Jean-Christophe Filliatre <Jean-Christophe.Filliatre@l...>
Subject: Re: [Caml-list] A (much less) frustrated beginner

james woodyatt writes:
 > For extra credit, have a look at the following bit of weirdness 
 > (though, I really think this thread belongs on the ocaml-beginners 
 > list).  I think you will be surprised at how fast it runs, and I 
 > suspect it would be a good piece of example code for figuring out how 
 > to use lazy evaluation in Ocaml.

Here  is another using  streams (thus  you need  to compile  with "-pp
camlp4o"). It's  a nice example to  learn about streams,  though it is
not that efficient.

let rec filter p = parser
  [< 'n; s >] -> if p n then [< 'n; filter p s >] else [< filter p s >]

let naturals = let rec gen n = [< 'n; gen (succ n) >] in gen 2

let primes =
  let rec sieve = parser
    [< 'n; s >] -> [< 'n; sieve (filter (fun m -> m mod n <> 0) s) >]
  sieve naturals

let _ = 
  while true do
    Printf.printf "%d\n" ( primes); flush stdout;

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: