Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #133958
| From | minforth@gmx.net (minforth) |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: LOOP |
| Date | 2025-07-03 19:33 +0000 |
| Organization | novaBBS |
| Message-ID | <f404de58a716f2f63cf31477da3014ae@www.novabbs.com> (permalink) |
| References | (5 earlier) <mc77onF8d4vU1@mid.individual.net> <2025Jun28.122351@mips.complang.tuwien.ac.at> <1be678489d752a940e171d426000dc17ed1ac471@i2pn2.org> <103pht8$121l9$1@dont-email.me> <2025Jun28.230148@mips.complang.tuwien.ac.at> |
On Sat, 28 Jun 2025 21:01:48 +0000, Anton Ertl wrote:
> sean@conman.org writes:
>> What is the difference between FOR/NEXT and DO/LOOP? Don't they do the
>>same thing?
>
> FOR ... NEXT on one system does not do the same thing as FOR ... NEXT
> on some other systems, and they all behave different from DO ... LOOP.
>
Correct. Here are variants with iterators that even run on gforth 0.7.9:
\ ====== <n> FOR# .. #TIMES
==================================================
\ original: machine code
\ demo variant: slow Forth
: _ITERATE \ end xt
swap
BEGIN dup 0>
WHILE over execute 1-
REPEAT 2drop ;
: FOR# postpone [: ; IMMEDIATE
: #TIMES postpone ;] postpone _iterate ; IMMEDIATE
\ ====== <n> FOR .. N M .. NEXT
==============================================
\ original: machine code with circular iteration control stack
\ advantage: avoids UNLOOP and doesn't clutter rstack
\ demo variant: slow Forth, indices on rstack like DO..LOOP, no
advantage
\ for gforth:
: rpick rp@ swap 1+ cells + @ ;
: N 2 rpick ; ( -- inner-index )
: M 6 rpick ; ( -- outer-index )
: _N-ITERATE \ end xt --
swap >r 0 >r
BEGIN 2r@ u> \ end n f
WHILE dup execute r> 1+ >r
REPEAT 2r> 2drop drop ;
: FOR postpone [: ; IMMEDIATE
: NEXT postpone ;] postpone _n-iterate ; IMMEDIATE
\ ====== <array> <#el> <elsize> FOR{ .. N M .. }NEXT
=========================
\ original: machine code with circular iteration control stack
\ advantage: avoids UNLOOP and doesn't clutter rstack
\ demo variant: slow Forth, indices on rstack like DO..LOOP, no
advantage
: _ARR-ITERATE \ adr els step xt --
>r dup >r * over + swap 2r> 2swap 2>r \ xt size -- s | r: end arr
BEGIN 2r@ u>
WHILE over execute dup r> + >r
REPEAT 2r> 2drop 2drop ;
: FOR{ postpone [: ; IMMEDIATE
: }NEXT postpone ;] postpone _arr-iterate ; IMMEDIATE
\ --- Tests
: STARS
20 FOR# '*' emit #TIMES ;
: NUMBER1
10 FOR n . NEXT ;
: NUMBER2
3 FOR cr 4 FOR n m 1+ * . NEXT NEXT ;
: TYPE-CHARARRAY \ a u --
dup >r pad swap move
pad r> 1 FOR{ n c@ emit }NEXT ;
CR STARS
CR NUMBER1
CR NUMBER2
CR S" Fortune" TYPE-CHARARRAY
--
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar
OOS approach revisited zbigniew2011@gmail.com (LIT) - 2025-06-23 05:09 +0000
Re: OOS approach revisited dxf <dxforth@gmail.com> - 2025-06-24 23:28 +1000
Re: OOS approach revisited zbigniew2011@gmail.com (LIT) - 2025-06-26 17:27 +0000
Re: OOS approach revisited minforth@gmx.net (minforth) - 2025-06-27 02:16 +0000
Re: OOS approach revisited dxf <dxforth@gmail.com> - 2025-06-27 17:29 +1000
Re: OOS approach revisited minforth <minforth@gmx.net> - 2025-06-27 11:49 +0200
Re: OOS approach revisited zbigniew2011@gmail.com (LIT) - 2025-06-27 16:55 +0000
Re: OOS approach revisited albert@spenarnc.xs4all.nl - 2025-06-27 20:15 +0200
Re: OOS approach revisited minforth <minforth@gmx.net> - 2025-06-27 22:35 +0200
Re: OOS approach revisited albert@spenarnc.xs4all.nl - 2025-06-28 11:34 +0200
Re: OOS approach revisited Stephen Pelc <stephen@vfxforth.com> - 2025-06-28 09:37 +0000
Re: OOS approach revisited dxf <dxforth@gmail.com> - 2025-06-28 12:03 +1000
LOOP (was: OOS approach revisited) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2025-06-28 10:23 +0000
Re: LOOP (was: OOS approach revisited) albert@spenarnc.xs4all.nl - 2025-06-28 14:26 +0200
Re: LOOP dxf <dxforth@gmail.com> - 2025-06-28 22:41 +1000
Re: LOOP sean@conman.org - 2025-06-28 20:04 +0000
Re: LOOP anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2025-06-28 21:01 +0000
Re: LOOP minforth@gmx.net (minforth) - 2025-07-03 19:33 +0000
Re: LOOP Gerry Jackson <do-not-use@swldwa.uk> - 2025-07-07 07:54 +0100
Re: LOOP minforth <minforth@gmx.net> - 2025-07-07 10:46 +0200
Re: LOOP dxf <dxforth@gmail.com> - 2025-06-29 13:04 +1000
Re: LOOP (was: OOS approach revisited) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2025-06-28 16:04 +0000
DO..LOOP and stack shuffling (was: OOS approach revisited) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2025-06-28 17:46 +0000
Re: DO..LOOP and stack shuffling Hans Bezemer <the.beez.speaks@gmail.com> - 2025-07-01 13:53 +0200
Re: DO..LOOP and stack shuffling dxf <dxforth@gmail.com> - 2025-07-03 13:59 +1000
Re: DO..LOOP and stack shuffling anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2025-07-03 07:50 +0000
Re: OOS approach revisited Hans Bezemer <the.beez.speaks@gmail.com> - 2025-06-30 15:43 +0200
Re: OOS approach revisited dxf <dxforth@gmail.com> - 2025-06-27 13:39 +1000
csiph-web