From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id WAA31487; Tue, 23 Dec 2003 22:13:54 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA31305 for ; Tue, 23 Dec 2003 22:13:52 +0100 (MET) Received: from dell.nogin.org (charter-242-037.caltech.edu [131.215.242.37]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id hBNLDpv15910 for ; Tue, 23 Dec 2003 22:13:51 +0100 (MET) Received: from nogin.org (localhost.localdomain [127.0.0.1]) by dell.nogin.org (8.12.10/8.12.9) with ESMTP id hBNLDnsu018783 for ; Tue, 23 Dec 2003 13:13:50 -0800 Message-ID: <3FE8B00D.4030901@nogin.org> Date: Tue, 23 Dec 2003 13:13:49 -0800 From: Aleksey Nogin User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6a) Gecko/20031023 X-Accept-Language: ru, en-us, en MIME-Version: 1.0 To: Caml List Subject: Re: [Caml-list] A (much less) frustrated beginner References: <1072206032.62516.27.camel@tylere> In-Reply-To: <1072206032.62516.27.camel@tylere> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 00,:99 primes:01 isprime:01 primes:01 printf:01 printf:01 isprime:01 626:99 int:01 int:01 rec:01 rec:01 exists:01 exists:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On 23.12.2003 11:00, Tyler Eaves wrote: > (* primes2.ml > Prime number generator, take 2 > Tyler Eaves > *) > > 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