Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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.
-- 
Jean-Christophe

======================================================================
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) >]
  in
  sieve naturals

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

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners