RE: for loops with stride

From: Don Syme (dsyme@microsoft.com)
Date: Fri Dec 11 1998 - 16:16:18 MET


From: Don Syme <dsyme@microsoft.com>
To: "'John Whitley'" <whitley@cse.buffalo.edu>, caml-list@inria.fr
Subject: RE: for loops with stride
Date: Fri, 11 Dec 1998 07:16:18 -0800

> On a hopefully less controversial note, I'd like to make a suggestion
> for an addition to OCaml for-loops. Consider an optional stride
> parameter, as in:
>
> for i = 0 to n-1 by stride do
> ...
> done

Well, this will no doubt bring controversy, but why not extend the syntax to
allow more general for loops? E.g.

for (i=0; i<n; i <- i+stride) do
   ...
done;

and in general

for (j1=e1 and ... and jn=en ; <--- declaration
     eg[j1...jn]; <--- guard
     j1 <- e1'[j1...jn]; ... ; jn <- en'[j1...jn]) <--- new values
do
  eb[j1,...,jn]
done;

which would be behaviourally equivalent to:

let j1 = ref e1
...
and jn = ref en in
while eg[(!j1), ..., (!jn)] do
  eb[(!j1), ..., (!jn)];
  j1 := e1'[(!j1),...,(!jn)];
  ...
  jn := en'[(!j1),...,(!jn)];
done;

where exp[exps] indicates textual substitution throughout the expression
(respecting name capture of course) for the obvious variables. No other
modification or derefencing of j1...jn would be allowed in the body of the
loop. Whether the assignment at the end should be simultaneous or not I'm
not sure - probably.

This would allow variable to range over types besides integers, and while I
personally would prefer to abstract iteration using higher order functions
there are plenty of people who might want to write:
  for (i=<list> ; not (null i); i <- tl i) do
      print i;
  done;

and for one-off iteration strategies I would probably prefer this anyway,
e.g.
  for (i=<list> ; not (null i) & not (null(tl i)); i <- tl(tl i)) do
      print i;
  done;
doesn't really deserve a special higher-order iterator.

Ex-C programmers would certainly like this, at least until they got into the
higher-order swing of things, and it seems pretty declarative to me.

Don

------------------------------------------------------------------------
At the lab: At home:
Microsoft Research Cambridge 11 John St
St George House CB1 1DT
Cambridge, CB2 3NH, UK
Ph: +44 (0) 1223 744797 Ph: +44 (0) 1223 722244
http://research.microsoft.com/users/dsyme
email: dsyme@microsoft.com
   "You've been chosen as an extra in the movie
        adaptation of the sequel to your life" -- Pavement, Shady Lane
------------------------------------------------------------------------



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:17 MET