Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] C style for loop
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jeff Henrikson <jehenrik@y...>
Subject: [Caml-list] C style for loop
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. . .


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

desugars into:

let rec iter_gensymXXX c =
  (* bla *)
  let c = (c+1) in
    if (c<10) then iter_gensymXXX c else ()
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 *)

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 ()
 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:  FAQ:
To unsubscribe, mail  Archives: