|
Having recently put together some slides for a Dylan presentation, I have to
question this characterization of the Dylan ‘for’ macro in particular. I think
Dylan ‘for’ macro is fairly straightforward and provides just enough options to
make it convenient to use as an alternative to writing out most common
iterations in “longhand”.
Granted, the full DRM description of evaluation and termination testing order
is pretty detailed (necessarily so, since it’s the language definition, not a
tutorial), but I think the behaviors make sense when distilled. Can you
describe in more detail what you think is “chrome” in Dylan’s ‘for’?
Here’s my description of it, adapted from my slides:
for (tree in forest)
look-at( tree )
end for;
for (i from 1 to 10) ...
for (j from 0 below 10,
k from 10 above 0 by -1) ...
for (thing = first-thing then next(thing),
until: done?(thing)) ...
It supports three different kinds of iteration and an explicit termination test,
and multiple iterations can be performed in parallel (the loop exits when any
of the iterations ends or the explicit test matches).
1. Collection iteration clause: ‘in’ iterates over every element in a collection.
2. Numeric iteration clause: ‘from’ iterates by integral values from a start
value up or down to an end value. ‘to’ is inclusive, ‘below’ and ‘above’ are
exclusive (the iteration stops before reaching the bounding value). ‘by’
optionally specifies how much to change the value each iteration; 1 is the
default.
3. Explicit Step iteration clause: ‘=’ assigns an initial value and the ‘then’
expression is evaluated each time through the loop to determine the next
value.
Termination clauses: One of ‘until:’ or ‘while:’ can be given to supply an
explicit termination test expression.
Taken together, an explicit step iteration and ‘while:’ termination clause are
like C/C++’s three for loop clauses.
--
Chris Page
|