Loops/Increment loop index within loop body: Difference between revisions

m
→‎With indexed loop word: tweaked text and code layout
m (→‎With indexed loop word: Change of heart re behaviours of incr and end)
m (→‎With indexed loop word: tweaked text and code layout)
Line 4,154:
===With indexed loop word===
 
Quackery has an iterative looping word that can change its step size mid-iteration (see [[Loops/For with a specified step#Quackery]]) but it is not well suited to this task. A better solution (i.e. more idiomatic) is to define a new looping word, <code>from</code>, that exactly meets the specification of the task.
 
<syntaxhighlight lang="Quackery"> [ stack ] is f.action ( --> s )
[ stack ] is f.end ( --> s )
[ stack ] is f.incr ( --> s )
[ stack ] is f.index ( --> s )
 
[ ' [ f.action f.end
f.incr f.index ] ] is f.stacks ( --> [ )
 
[ f.index share ] is index ( --> n )
 
[ f.incr replace ] is incr ( n --> )
 
[ f.end replace ] is end ( b --> )
 
[ 1 false ]'[
Line 4,177:
1 incr
f.end share until ]
f.stacks witheach release ] is from ( n --> )</syntaxhighlight>
f.stacks
witheach release ] is from ( n --> )
 
</syntaxhighlight>
 
<code>from</code> takes its initial index from the stack, and performs the nest that follows it repeatedly until the ending condition is satisfied, incrementing the index at the end of each iteration.
Line 4,205 ⟶ 4,202:
dip [ char , join ]
again ]
join reverse echo$ ] is echo, ( n --> )
 
0
1,462

edits