English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
RE: for loops with stride
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1998-12-14 (08:47)
From: Don Syme <dsyme@m...>
Subject: RE: for loops with stride

> 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

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

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)];

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; 

and for one-off iteration strategies I would probably prefer this anyway,
  for (i=<list> ; not (null i) & not (null(tl i)); i <- tl(tl i)) do 
      print i; 
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.  


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
email: dsyme@microsoft.com
   "You've been chosen as an extra in the movie
        adaptation of the sequel to your life"  -- Pavement, Shady Lane