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 IAA30024; Tue, 23 Dec 2003 08:23:39 +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 IAA30221 for ; Tue, 23 Dec 2003 08:23:38 +0100 (MET) Received: from out2.smtp.messagingengine.com (out2.smtp.messagingengine.com [66.111.4.26]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id hBN7Nbv20564 for ; Tue, 23 Dec 2003 08:23:37 +0100 (MET) X-Sasl-enc: nMus2KOol8XodWOH5auiPw 1072164216 Received: from [192.168.1.101] (user224.net229.nc.sprint-hsd.net [65.173.93.224]) by mail.messagingengine.com (Postfix) with ESMTP id A958B487E5D for ; Tue, 23 Dec 2003 02:23:35 -0500 (EST) Mime-Version: 1.0 (Apple Message framework v609) In-Reply-To: References: <1072152186.59938.30.camel@tylere> <0D3B5ECF-350D-11D8-A8F7-000393CB0F1E@spy.net> Content-Type: multipart/mixed; boundary=Apple-Mail-1--632267126 Message-Id: From: Tyler Eaves Subject: Re: [Caml-list] Frustrated Beginner Date: Tue, 23 Dec 2003 02:23:33 -0500 To: caml-list@inria.fr X-Mailer: Apple Mail (2.609) X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 invocation:01 55.:99 inefficient:01 rec:01 rec:01 syntax:02 match:02 o'caml:02 overhead:03 wrote:03 wrote:03 tail:03 scheme:03 let:04 X-Attachments: type="application/octet-stream" name="primes.ml" name="primes.ml" Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk --Apple-Mail-1--632267126 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed On Dec 23, 2003, at 1:53 AM, Dustin Sallings wrote: > > On Dec 22, 2003, at 22:26, Tyler Eaves wrote: > > (I'm sending this back to the list because, as you pointed out, there > were a few problems with that example, so here's one for the > archives). > >> Okay, so far so good. I take it that in O'Caml the overhead of a >> function call is much less than in, say, C? Otherwise this approach >> would seem to be rather inefficient. > > Well, the short answer is that that's not a function call. :) See > tail-recursion. > > It's also not the best example since it was so simple and didn't > return anything useful. Consider this implementation of fold (roughly > translating it in this email from a scheme version I wrote on my > palm): > > let rec fold f init lst = > match lst with > [] -> init > | h::t -> fold f (f init h) t > ;; > > If you're not familiar with fold, here's an example: > > fold (fun v c -> c + v) 0 [1;2;3;4;5;6;7;8;9;10] > > That invocation returns 55. Basically, the function is called for > each value in the list (v) and the current value is passed in along > with the current value during the calculation (c). When it gets to > the end (list is []), you return the init value. Otherwise, you apply > the function. > > Note that this function could also be written as follows (and > probably would be): > > let rec fold f init = function > [] -> init > | h::t -> fold f (f init h) t > ;; This makes since in general. I'm guess h and t correspond to the head and tail of the lift. It'll probably make more sense tomorrow. It's 2:11 here, and I'd planned to be asleep two hours ago. The replies have kept me up ;) Based on what I've learned, I've tried to write a simple prime number finder. It doesn't actually *work* or anything (Syntax Error on line 23, can't figu --Apple-Mail-1--632267126 Content-Transfer-Encoding: 7bit Content-Type: application/octet-stream; x-unix-mode=0644; name="primes.ml" Content-Disposition: attachment; filename=primes.ml (* primes.ml build a list of prime numbers. Tyler Eaves Very naive and stupid algortihmn. It tries taking the modulo of each known prime with the test number. If modulo = 0 for any number, the test is not prime. *) let primes = [2];; (* Modulo tester *) let fmod x y = x mod y;; let rec isprime n = ( let test = List.map (fmod n) primes; let testa = Array.of_list test; let testa = Array.sort compare testa; if testa.(0) = 0 then (* Not prime *) Printf.printf "%d is not prime.\n" n else begin Printf.printf "%d IS PRIME!!\n" n; let primes = n :: primes end ); isprime n+1 ;; isprime 3;; --Apple-Mail-1--632267126 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed re out why...), but I think I've figured some of this stuff out, kinda. I'm encouraged at least :) --Apple-Mail-1--632267126-- ------------------- 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