From: John Prevost <jprevost@panasas.com>
To: "Jeff Henrikson" <jehenrik@yahoo.com>
Cc: "Chris Hecker" <checker@d6.com>, <caml-list@inria.fr>,
"Berke Durak" <berke@altern.org>
Subject: Re: [Caml-list] Some suggested improvements to the Graphics and Bigarray modules
Date: 10 Oct 2001 20:50:28 -0400 [thread overview]
Message-ID: <jkvghn9ezv.fsf@kinsman.panasas.com> (raw)
In-Reply-To: <003c01c151c4$6ba686c0$0b01a8c0@mit.edu>
>>>>> "jh" == Jeff Henrikson <jehenrik@yahoo.com> 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
next prev parent reply other threads:[~2001-10-11 8:39 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-10-09 21:31 Berke Durak
2001-10-09 23:21 ` [Caml-list] " Christopher Quinn
2001-10-10 0:52 ` [Caml-list] " Jeff Henrikson
2001-10-10 7:04 ` Chris Hecker
2001-10-10 19:47 ` Jeff Henrikson
2001-10-11 0:50 ` John Prevost [this message]
2001-10-12 14:29 ` Jun P. FURUSE
2001-10-12 15:02 ` Xavier Leroy
2001-10-12 15:38 ` Berke Durak
2001-10-12 17:15 ` Daniel de Rauglaudre
2001-10-13 23:16 ` Berke Durak
2001-10-14 3:14 ` Daniel de Rauglaudre
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=jkvghn9ezv.fsf@kinsman.panasas.com \
--to=jprevost@panasas.com \
--cc=berke@altern.org \
--cc=caml-list@inria.fr \
--cc=checker@d6.com \
--cc=jehenrik@yahoo.com \
/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