Loops/Foreach: Difference between revisions

Line 1,274:
cells bounds do i @ . cell +loop ; \ 3 2 1</lang>
===FOREACH===
<lang forth>\ This thing about extendableextensible 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.
 
<lang forth>: FOREACH ( array size XT --)
>R \ save execution token on return stack
CELLS BOUNDS \ covert addr len -> last first addresses
Line 1,287:
CELL+ \ move first to the next memory cell
REPEAT
R> DROP \ clean return stack
2DROP \ and data stack
;
 
Line 1,297:
 
\ Usage example:
A[] 10 ' ? FOREACH </lang>
 
=={{header|Fortran}}==
Anonymous user