<?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/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"
  prev="2003/12/5523210d21c4e1436e65cb86585dda2e"
  next="2003/12/60a121b77b89d2c770a1594f0eefeb64"
  prev-in-thread="2003/12/5523210d21c4e1436e65cb86585dda2e"
  next-in-thread="2003/12/60a121b77b89d2c770a1594f0eefeb64"
  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 Wed, Dec 24, 2003 at 10:04:49AM +0900, Nicolas Cannasse wrote:
&gt; &gt; I think I'm beginning to get it. Attached find my second try at a prime
&gt; &gt; number generator.
&gt; &gt;
&gt; &gt; This one is distinguished from my first by two important differences:
&gt; &gt;
&gt; &gt; 1: It is written (I think) in a completly functional style
&gt; &gt; 2: It actually *works*. It's pretty fast too. Running as an optimized
&gt; &gt; binary on my system (Athlon @ 950mhz under FreeBSD 5.1) it can calculate
&gt; &gt; the first 15104 primes (Highest is 165161) with one minute of CPU time.
&gt; &gt;
&gt; &gt; Thanks again for all your help!
&gt; 
&gt; Check the difference with this one , getting rid of the exceptions :
&gt; 
&gt; let rec isprime n primes =
&gt;     let rec test = function
&gt;         | [] -&gt; true
&gt;         | x :: l -&gt;
&gt;             if n mod x = 0 then
&gt;                 false
&gt;             else if sqrt (float_of_int n) &lt; (float_of_int x) then
&gt;                 true
&gt;             else
&gt;                 loop l
&gt;     in
&gt;     if test primes then begin
&gt;         Printf.printf "%d is PRIME!\n" n;
&gt;         isprime (n+2) (primes @ [n])
&gt;     end else
&gt;         isprime (n+2) primes
&gt; 
&gt; there is another improvement :
&gt; @ is linear, so is not efficient here.
&gt; we could write  ( n :: primes ) the append the element at the head of the
&gt; list (in constant time) but then the list will be reverse constructed.
&gt; One trick is to manually modify the list using low-level - undocumented and
&gt; unsafe Obj operations so we can construct the list directly in the good
&gt; order, but that require some hacks.

Don't teach Obj tricks to a beginner, that is not the way to go. Usually
you just need to reverse the list at the end of the construction, which
is enough, and since both the :: and the reversal are linear.

Friendly,

Sven Luther

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

