English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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" (Stream.next primes); flush stdout;

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