Loops/Increment loop index within loop body: Difference between revisions
Content added Content deleted
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: | Line 4,154: | ||
===With indexed loop word=== |
===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 that exactly meets the specification of the task. |
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 ) |
<syntaxhighlight lang="Quackery"> [ stack ] is f.action ( --> s ) |
||
[ stack ] is f.end ( --> s ) |
[ stack ] is f.end ( --> s ) |
||
[ stack ] is f.incr ( --> s ) |
[ stack ] is f.incr ( --> s ) |
||
[ stack ] is f.index ( --> s ) |
[ stack ] is f.index ( --> s ) |
||
[ ' [ f.action f.end |
[ ' [ f.action f.end |
||
f.incr f.index ] ] is f.stacks ( --> [ ) |
f.incr f.index ] ] is f.stacks ( --> [ ) |
||
[ f.index share ] is index ( --> n ) |
[ f.index share ] is index ( --> n ) |
||
[ f.incr replace ] is incr ( n --> ) |
[ f.incr replace ] is incr ( n --> ) |
||
[ f.end replace ] is end ( b --> ) |
[ f.end replace ] is end ( b --> ) |
||
[ 1 false ]'[ |
[ 1 false ]'[ |
||
Line 4,177: | Line 4,177: | ||
1 incr |
1 incr |
||
f.end share until ] |
f.end share until ] |
||
⚫ | |||
f.stacks |
|||
⚫ | |||
</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. |
<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: | Line 4,202: | ||
dip [ char , join ] |
dip [ char , join ] |
||
again ] |
again ] |
||
join reverse echo$ ] |
join reverse echo$ ] is echo, ( n --> ) |
||
0 |
0 |