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 HAA29087; Tue, 23 Dec 2003 07:53:39 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id HAA29527 for ; Tue, 23 Dec 2003 07:53:38 +0100 (MET) Received: from bob.west.spy.net (mail.west.spy.net [66.149.231.226]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id hBN6rab29522 for ; Tue, 23 Dec 2003 07:53:36 +0100 (MET) Received: from [192.168.1.50] (dustinti.west.spy.net [192.168.1.50]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (Client did not present a certificate) by bob.west.spy.net (Postfix) with ESMTP id 7F21E59CD; Mon, 22 Dec 2003 22:53:34 -0800 (PST) In-Reply-To: References: <1072152186.59938.30.camel@tylere> <0D3B5ECF-350D-11D8-A8F7-000393CB0F1E@spy.net> Mime-Version: 1.0 (Apple Message framework v609) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: Content-Transfer-Encoding: 7bit Cc: Caml List From: Dustin Sallings Subject: Re: [Caml-list] Frustrated Beginner Date: Mon, 22 Dec 2003 22:53:30 -0800 To: Tyler Eaves X-Mailer: Apple Mail (2.609) X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 invocation:01 55.:99 argv:01 char:01 sem:99 cae:99 inefficient:01 rec:01 rec:01 match:02 her:97 o'caml:02 worse:03 overhead:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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 ;; > Would not Stream.iter be worth considering here? I found that when > looking at at the Stream doc. Absolutely. I haven't typically used streams that way. How 'bout a rewrite: let f = open_in Sys.argv.(1) in (* Loop on this stream *) Stream.iter (function '\t' -> print_string " - tab - " | c -> print_char c ) (Stream.of_channel f); close_in f >> (* The previous statement had a sem because it's an imperative >> style here *) >> close_in f > Will this close EVER be reached? I see only two possibilities, either > the stream eventually EOFs, or we've hit an 'infinite' file, such as > /dev/random Good catch. :) I think that was another example of using streams a bit different from how I usually do, but bad examples are worse than no examples. Or maybe it was one of those ``find the bug'' assignments. :) -- SPY My girlfriend asked me which one I like better. pub 1024/3CAE01D5 1994/11/03 Dustin Sallings | Key fingerprint = 87 02 57 08 02 D0 DA D6 C8 0F 3E 65 51 98 D8 BE L_______________________ I hope the answer won't upset her. ____________ ------------------- 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