caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Sven Luther <sven.luther@wanadoo.fr>
To: Nicolas Cannasse <warplayer@free.fr>
Cc: Tyler Eaves <tyler@ml1.net>, caml-list@inria.fr
Subject: Re: [Caml-list] A (much less) frustrated beginner
Date: Wed, 24 Dec 2003 07:23:40 +0100	[thread overview]
Message-ID: <20031224062340.GA2923@iliana> (raw)
In-Reply-To: <005001c3c9b9$ee438140$0274a8c0@PWARP>

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


  reply	other threads:[~2003-12-24  6:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-23 19:00 Tyler Eaves
2003-12-23 21:13 ` Aleksey Nogin
2003-12-24  1:04 ` Nicolas Cannasse
2003-12-24  6:23   ` Sven Luther [this message]
2003-12-24  6:42 ` james woodyatt
2003-12-24  8:53   ` Jean-Christophe Filliatre

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20031224062340.GA2923@iliana \
    --to=sven.luther@wanadoo.fr \
    --cc=caml-list@inria.fr \
    --cc=tyler@ml1.net \
    --cc=warplayer@free.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).