caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* RE: for loops with stride
@ 1998-12-11 15:16 Don Syme
  0 siblings, 0 replies; 2+ messages in thread
From: Don Syme @ 1998-12-11 15:16 UTC (permalink / raw)
  To: 'John Whitley', caml-list


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




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

* for loops with stride
@ 1998-12-10  3:21 John Whitley
  0 siblings, 0 replies; 2+ messages in thread
From: John Whitley @ 1998-12-10  3:21 UTC (permalink / raw)
  To: caml-list


Thanks to everyone who responded to my earlier question re:
composition.  Quite enlightening discussion...  8-)

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

At least in certain signal processing code that I'm working with
presently, the above is a common (and highly convenient) pattern.  The
corresponding while loop is simply not as easy to write or understand.

Thanks,
John




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

end of thread, other threads:[~1998-12-14  8:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-12-11 15:16 for loops with stride Don Syme
  -- strict thread matches above, loose matches on Subject: below --
1998-12-10  3:21 John Whitley

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