Re: length() and looping
- Posted by jimcbrown (admin) Mar 13, 2017
- 1307 views
Most languages get around this problem by allowing one to declare the type of the index; but, not Euphoria (it MUST be an atom).
Many years ago I wrote a preprocessor for RDS Euphoria called Dredge. One feature it added was the wfor loop. (Short for "While-implemented FOR loop".) In the earliest version, this is how it worked:
You'd code it like this:
integer i wfor i = 1.23 + to_integer(23) to 77.3 /* by 2.15 */ do ... end wfor
and it'd convert it to this:
integer i i = 1.23 + to_integer(23) while i < 77.3 do ... i += 1 /* or, if using by keyword, then i += 2.15 */ end while
Note, however, that this by itself would have been illegal:
wfor i = 1.23 + to_integer(23) to 77.3 do ... end wfor
You were required to declare the variable to use in the loop in advance. (Today, that would no longer be true because of 4.0+'s support for forward referencing - but you would still have to declare it somewhere in the same scope as the loop.)
The primary use of this contruct was because, back then, RDS Euphoria did not allow you to manually set the value of the loop iterator in a for loop. (Today, 4.0+ allows this.) The requirement to manually declare the iterator first was just a result of the different semantics between the for loop (which had automatic variable creation) and the while loop (which did not).
for i = 1.999 + to_integer(1) to 30/3.0 by ceil(2.15) do integer j = to_integer(i) ? {i,j,round(i)} end for
None of this really matters. It is what it is. Sometimes I get caught!
Thanks again,
jd
Even wfor wouldn't provide perfect protection here. The above (without the 'protection' you added) wouldn't work in a wfor with an integer variable, but I suspect that this might:
--on 32bit constant nan=-(2*power(10,308)/(3*power(10,308))), inf=2*power(10,308) wfor i = 1 to nan by 2 do ... end wfor