<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/12/d532f069cb0e2459bc1c0974c321c7b3"
  from="Aleksey Nogin &lt;aleksey@n...&gt;"
  author="Aleksey Nogin"
  date="2003-12-23T21:13:54"
  subject="Re: [Caml-list] A (much less) frustrated beginner"
  prev="2003/12/7511a52ffbb5f995bcc51cc1619bb2af"
  next="2003/12/5f24e60286352a813888ef91b50b6578"
  prev-in-thread="2003/12/0669d0f53998ae1f3b5da005b4fd9200"
  next-in-thread="2003/12/5523210d21c4e1436e65cb86585dda2e"
  prev-thread="2003/12/6f29bbcbd7940b1f3bee84f95924c267"
  next-thread="2003/12/68fb62727bb19539682c9f2f2f52efda"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] A (much less) frustrated beginner">
<msg 
  url="2003/12/0669d0f53998ae1f3b5da005b4fd9200"
  from="Tyler Eaves &lt;tyler@m...&gt;"
  author="Tyler Eaves"
  date="2003-12-23T18:58:56"
  subject="[Caml-list] A (much less) frustrated beginner">
<msg 
  url="2003/12/d532f069cb0e2459bc1c0974c321c7b3"
  from="Aleksey Nogin &lt;aleksey@n...&gt;"
  author="Aleksey Nogin"
  date="2003-12-23T21:13:54"
  subject="Re: [Caml-list] A (much less) frustrated beginner">
</msg>
<msg 
  url="2003/12/5523210d21c4e1436e65cb86585dda2e"
  from="Nicolas Cannasse &lt;warplayer@f...&gt;"
  author="Nicolas Cannasse"
  date="2003-12-24T01:05:01"
  subject="Re: [Caml-list] A (much less) frustrated beginner">
<msg 
  url="2003/12/dc3cc05d859914889409e9a3c96de1e9"
  from="Sven Luther &lt;sven.luther@w...&gt;"
  author="Sven Luther"
  date="2003-12-24T06:23:42"
  subject="Re: [Caml-list] A (much less) frustrated beginner">
</msg>
</msg>
<msg 
  url="2003/12/60a121b77b89d2c770a1594f0eefeb64"
  from="james woodyatt &lt;jhw@w...&gt;"
  author="james woodyatt"
  date="2003-12-24T06:42:13"
  subject="Re: [Caml-list] A (much less) frustrated beginner">
<msg 
  url="2003/12/64fa006e243b740a6a8ada2542e92b5c"
  from="Jean-Christophe Filliatre &lt;Jean-Christophe.Filliatre@l...&gt;"
  author="Jean-Christophe Filliatre"
  date="2003-12-24T08:59:59"
  subject="Re: [Caml-list] A (much less) frustrated beginner">
</msg>
</msg>
</msg>
</thread>

<contents>
On 23.12.2003 11:00, Tyler Eaves wrote:

&gt; (*  primes2.ml 
&gt;     Prime number generator, take 2
&gt;     Tyler Eaves &lt;tyler@ml1.net&gt;
&gt;     *)
&gt;    
&gt; exception Not_prime;;
&gt; exception Its_Prime;;
&gt; 
&gt; let rec isprime n primes = (
&gt;     try
&gt;         List.iter (fun x -&gt; if n mod x = 0 then raise Not_prime else if sqrt
&gt;         (float_of_int n) &lt; (float_of_int x) then raise Its_Prime else ()) primes;
&gt;         Printf.printf "%d is PRIME!\n" n; 
&gt;         isprime (n+2) (List.concat [primes; [n]])
&gt;     with 
&gt;         Not_prime -&gt; isprime (n+2) primes
&gt;         | Its_Prime -&gt; (Printf.printf "%d is PRIME!\n" n; 
&gt;           isprime (n+2) (List.concat [primes; [n]]))
&gt;     );;
&gt; 
&gt; isprime 3 [2];;

Minor thing - instead of "List.concat [primes; [n]]", you can write just 
"primes @ [n]" ("@" is an infix list append operator).

Also, List.iter is still somewhat imperative. Here is a slight modification:

let rec list_exists f n = function
    [] -&gt; false
  | hd :: _ when hd &gt; n -&gt; false
  | hd :: tl -&gt; f hd &amp;&amp; (list_eists f n tl) ;;

let rec print_primes n primes =
    let limit = int_of_float (sqrt (float_of_int n))) in
    if list_exists (fun x -&gt; n mod x = 0) limit primes then
       print_primes (n + 2) primes
    else begin
       Printf.printf "%d is PRIME!\n" n;
       print_primes (n+2) (primes @ [n])
    end;;

print_primes 3 [2];;

-- 
Aleksey Nogin

Home Page: http://nogin.org/
E-Mail: nogin@cs.caltech.edu (office), aleksey@nogin.org (personal)
Office: Jorgensen 70, tel: (626) 395-2907

-------------------
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

</contents>

</message>

