Loops/Foreach: Difference between revisions
Content added Content deleted
Line 1,274: | Line 1,274: | ||
cells bounds do i @ . cell +loop ; \ 3 2 1</lang> |
cells bounds do i @ . cell +loop ; \ 3 2 1</lang> |
||
===FOREACH=== |
===FOREACH=== |
||
This thing about extensible languages is if you need FOREACH, you can have FOREACH |
|||
The Forth ' operator returns the "execution token" (XT) of a Forth word. An XT can be run with EXECUTE. |
|||
If we apply an appropriate execution to all the elements of an array we have it. |
|||
: FOREACH ( array size XT --) |
<lang forth>: FOREACH ( array size XT --) |
||
>R \ save execution token on return stack |
>R \ save execution token on return stack |
||
CELLS BOUNDS \ covert addr len -> last first addresses |
CELLS BOUNDS \ covert addr len -> last first addresses |
||
Line 1,287: | Line 1,287: | ||
CELL+ \ move first to the next memory cell |
CELL+ \ move first to the next memory cell |
||
REPEAT |
REPEAT |
||
R> DROP \ clean return stack |
R> DROP \ clean return stack |
||
2DROP \ and data stack |
2DROP \ and data stack |
||
; |
; |
||
Line 1,297: | Line 1,297: | ||
\ Usage example: |
\ Usage example: |
||
A[] 10 ' ? FOREACH |
A[] 10 ' ? FOREACH </lang> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |