Generic Looping: Cutting FOR Back a Bit

Trying to convert code to be stackless is difficult, and the more fiddly code you have the harder it can be.

The implementation of FOR loops is rather complex, because it tries to support FOR looping over a series...which I'm vaguely aware it can do, but have never used. I'll point out that Red doesn't even have a FOR construct, much less one that iterates series. :-/

Anyway, here are some of the tests that there were for the feature:

``````    success: true | x: "a"
for i x tail of x 1 [continue | success: false]
success = true
---
out: copy ""
for i s: "abc" back tail of s 1 [append out i]
out = "abcbcc"
---
out: copy []
for i b: [1 2 3] back tail of b 1 [append out i]
out = [1 2 3 2 3 3]
---
success: true
for i b: [1] tail of :b -1 [success: false]
success = true
---
num: 0
for i (b: next [1]) (back b) 1 [num: num + 1 | break]
num = 0
---
([] = for i b: tail of [1] head of b -2 [i])
``````

It seems to me this is mostly covered by FOR-SKIP, which has specializations FOR-NEXT and FOR-BACK when you just want to skip by 1 or -1. It doesn't include the stopping point as part of the loop definition, but I don't know how often it really comes up...and not too hard to do yourself. I don't see there being all that many instances where the polymorphic nature of FOR is useful because "sometimes you're counting integers, and sometimes you're skipping in series".

So to expedite FOR becoming stackless, I'm cutting it down to just numeric types.

But if there are cases where a looping construct that spans integers and series is actually useful, I'd be interested to know what it is.

I'll also mention that I feel like REPEAT for integers is more clearly covered by COUNT-UP, which suggests the existence of its complement COUNT-DOWN. And for series it is more clearly covered by FOR-NEXT (which suggests the existence of its complements FOR-BACK and FOR-SKIP).

This brings back an age old debate about terminology. Repeating something doesn't really suggest a direction, just "do this thing a certain number of times". Thus I (still) think that REPEAT is a better fit for what LOOP does today:

``````repeat 5 [
print "Not needing a variable makes sense...repeat 5 times."
]
``````

Whereas the word LOOP is one of those highly general overarching terms that is short and easy to type, that seems better used for a much more general dialect (like how we think of PARSE and now QUERY).

For precedent, see the Lisp LOOP macro.