[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
FOR macros (was Re: Parodies make everyone mad)
[Just catching up on older messages, thus the 2002 quote date.]
On Nov 18, 2002, at 12:04, Eric Kidd wrote:
> 6) Do not, under any circumstance, write a LOOP or FOR macro
> that takes more than two pages of code to implement. Nobody
> understands all the frobs and gadgets in Common LISP's LOOP,
> and few people use more than a third of Dylan's FOR macro.
> ("Oooh, in what order should we evaluate the test-clauses
> if we include *both* a frob-clause *and* a gadget-clause?")
> Adding massive amounts of chrome to core language features
> is pure self-indulgence.
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 it’s 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 - Software Wrangler - palmOne, Inc.
Dylan + You = Code
<http://www.gwydiondylan.org/>
<http://www.cafepress.com/chrispage>