Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Aleksey Nogin <aleksey@nogin.org>
To: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] A (much less) frustrated beginner
Date: Tue, 23 Dec 2003 13:13:49 -0800	[thread overview]
Message-ID: <3FE8B00D.4030901@nogin.org> (raw)
In-Reply-To: <1072206032.62516.27.camel@tylere>

On 23.12.2003 11:00, Tyler Eaves wrote:

> (*  primes2.ml 
>     Prime number generator, take 2
>     Tyler Eaves <tyler@ml1.net>
>     *)
>    
> exception Not_prime;;
> exception Its_Prime;;
> 
> let rec isprime n primes = (
>     try
>         List.iter (fun x -> if n mod x = 0 then raise Not_prime else if sqrt
>         (float_of_int n) < (float_of_int x) then raise Its_Prime else ()) primes;
>         Printf.printf "%d is PRIME!\n" n; 
>         isprime (n+2) (List.concat [primes; [n]])
>     with 
>         Not_prime -> isprime (n+2) primes
>         | Its_Prime -> (Printf.printf "%d is PRIME!\n" n; 
>           isprime (n+2) (List.concat [primes; [n]]))
>     );;
> 
> 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
    [] -> false
  | hd :: _ when hd > n -> false
  | hd :: tl -> f hd && (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 -> 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


  reply	other threads:[~2003-12-23 21:13 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 [this message]
2003-12-24  1:04 ` Nicolas Cannasse
2003-12-24  6:23   ` Sven Luther
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=3FE8B00D.4030901@nogin.org \
    --to=aleksey@nogin.org \
    --cc=caml-list@inria.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