SMLFamily / Successor-ML

A version of the 1997 SML definition with corrections and some proposed Successor ML features added.
190 stars 10 forks source link

For - cycle? #28

Open const-rs opened 7 years ago

const-rs commented 7 years ago

What about including Ocaml-style for cycle:

for i = 1 to 18 do ... done (or end)

It is very handy when iterating through arrays or strings. Also, it does not let one make common mistake with repeating calls in condition, like

for( i = 0; i < strlen(s); i++)

JohnReppy commented 7 years ago

It is not a very functional programming style to use loops. Why not just define a combinator?

fun foreach (lo, hi) f = let
      fun lp i = if (i <= hi) then (f i; lp(i+1)) else ()
      in
        lp lo
      end

so you can write

foreach (1, 18) (fn i => ...)

If we are going to add syntax for loops, then I would be much more interested in list/vector comprehensions.