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 KAA19164; Thu, 11 Oct 2001 10:39:04 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA19158 for caml-list@pauillac.inria.fr; Thu, 11 Oct 2001 10:39:03 +0200 (MET DST) 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 CAA09188 for ; Thu, 11 Oct 2001 02:50:43 +0200 (MET DST) Received: from kinsman.panasas.com (gw2.panasas.com [65.194.124.178]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f9B0og112484 for ; Thu, 11 Oct 2001 02:50:42 +0200 (MET DST) Received: (from jprevost@localhost) by kinsman.panasas.com (8.9.3/8.9.3) id UAA18962; Wed, 10 Oct 2001 20:50:33 -0400 X-Authentication-Warning: kinsman.panasas.com: jprevost set sender to jprevost@panasas.com using -f To: "Jeff Henrikson" Cc: "Chris Hecker" , , "Berke Durak" Subject: Re: [Caml-list] Some suggested improvements to the Graphics and Bigarray modules References: <003c01c151c4$6ba686c0$0b01a8c0@mit.edu> From: John Prevost Date: 10 Oct 2001 20:50:28 -0400 In-Reply-To: <003c01c151c4$6ba686c0$0b01a8c0@mit.edu> Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.0.103 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk >>>>> "jh" == Jeff Henrikson writes: jh> Also, I find the caml for loop's lack of functionality jh> annoying. I really should learn camlp4 so I can write a real jh> C-style for loop. (with break and continue, though it's not jh> pertinent here.) Somebody doesn't have such things jh> convieniently lying around do they? Personally, I use tail loops for this sort of thing. You could also use a while loop, but that is less efficient than a for loop or a tail loop in O'Caml (since you'd have to use refs and break the write barrier.) Here's an example: C code: int i; int j; int count; /* what does this loop do? I don't know... */ for ( i = 0, count = 0; i < I_MAX; i++ ) { for ( j = 0; j < J_MAX; j++ ) { if ( i == j ) continue; if ( (i + j) == count ) break; count++; } } return count; Caml code: let rec loop_1 i count = let rec loop_2 j count = if i = j then loop_2 (succ j) count else if i + j = count then loop_1 (succ i) count else loop_2 (succ j) (succ count) in loop_2 0 count in loop_1 0 0 The caml code is certainly less clear in this case--but I think that's partialyl because the computation was created just to make a point. :) One might be able to use a ref for count to make it more clear how count is "updated". But it leads to even messier code, and worse runtime performance. A "real" loop example would also provide a way to define better names for the functions than "loop_1" and "loop_2". The tail calling to continue or break loops makes it easy to duplicate effects that would be created in C with gotos, since you can only break or continue the inner loop in C. With some experience, tail-call loops will come to mind naturally. I practically never use the for or while loop constructs in O'Caml. John. ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr