caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] C style for loop
@ 2001-10-11 13:34 Damien Doligez
  2001-10-11 13:43 ` Bruce Hoult
  0 siblings, 1 reply; 6+ messages in thread
From: Damien Doligez @ 2001-10-11 13:34 UTC (permalink / raw)
  To: caml-list

>At 2:47 PM +0200 11/10/01, Berke Durak wrote:

>>Do you really pretend that ``C-style for loops'' have ``self-evident
>>readability merits'' ?! My opinion is that ```C-style'' loop syntax
>>IS unreadable, ununderstandable and unprovable. How many people using
>>C know the _exact_ semantics of :
>>
>>	for(exp1;expr2;expr3){expr4}

>From: Bruce Hoult <bruce@hoult.org>

>Sure, it's easy:
>
>   {
>      exp1;
>      while (expr2){
>          expr4;
>          expr3;
>      }
>   }
>
>It's damn ugly, though, and with much unnecessary repetition of the 
>control variable in simple cases.

It's incorrect, too.  A "continue" statement within expr4 doesn't do
the same thing in both versions.

-- Damien
-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread
* [Caml-list] C style for loop
@ 2001-10-11  5:47 Jeff Henrikson
  2001-10-11  8:58 ` Daniel de Rauglaudre
  2001-10-11 12:47 ` Berke Durak
  0 siblings, 2 replies; 6+ messages in thread
From: Jeff Henrikson @ 2001-10-11  5:47 UTC (permalink / raw)
  To: caml-list, jprevost

Okay, so maybe I should be more specific about what I want in a "C-style for loop."  Its readablity merits are hopefully self
evident.  Well, unless you're a compulsive CPS addict who wishes even his grocery list could be written to tail recurse. . .

Suppose

(*  loop var | init val | while | expr for next val *)
for     c         0      (c<10)        (c+1)         do
  (* bla *)
done;

desugars into:

let rec iter_gensymXXX c =
  (* bla *)
  let c = (c+1) in
    if (c<10) then iter_gensymXXX c else ()
  in
iter_gensymXXX 0


Then we could write nice readable nested loops for square arrays, etc.  And then there's the canonical loop form which enters in a
place different from the exit test.  For that, C style "break" is a readable idiom:


for c 0 true c do
  (* bla1 *)
  let c = c+1 in
    if !(c<10) then break;
  (* bla2 *)
end;

could desugar into:

let rec iter_gensymXXX c =
  (* bla1 *)
  let c = c+1 in
    if !(c<10) then
	()
    else begin
      (* bla2 *)
	let c = c in
	  if true then iter_gensymXXX c else ()
    end
 in
 iter_gensymXXX 0


The transformation for "continue" is similar.

Pardon my laziness, camlp4 looks really cool, but I haven't taken the time to learn it.  For the time being, if somebody has
already written a macro kind of like this, I'll take it.  IMHO it would be a boon to the caml community to have a standard form for
this.  Even if it were only a "style guide" addition or what have you.


Jeff Henrikson


-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2001-10-11 13:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-11 13:34 [Caml-list] C style for loop Damien Doligez
2001-10-11 13:43 ` Bruce Hoult
  -- strict thread matches above, loose matches on Subject: below --
2001-10-11  5:47 Jeff Henrikson
2001-10-11  8:58 ` Daniel de Rauglaudre
2001-10-11 12:47 ` Berke Durak
2001-10-11 13:11   ` Bruce Hoult

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).