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 [thread overview]
Message-ID: <39ADCF833E74D111A2D700805F1951EF0B2EF6D1@RED-MSG-06> (raw)
> 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
------------------------------------------------------------------------
next reply other threads:[~1998-12-14 8:47 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-12-11 15:16 Don Syme [this message]
-- strict thread matches above, loose matches on Subject: below --
1998-12-10 3:21 John Whitley
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=39ADCF833E74D111A2D700805F1951EF0B2EF6D1@RED-MSG-06 \
--to=dsyme@microsoft.com \
--cc=caml-list@inria.fr \
--cc=whitley@cse.buffalo.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).