• Re: LOOP

    From sean@conman.org@21:1/5 to It was thus on Sat Jun 28 20:04:56 2025
    It was thus said that the Great dxf <dxforth@gmail.com> once stated:
    <[Wil] W.BADEN1> Chuck walked out of ANSI becuz it wudn't make
    FOR...NEXT required.
    <[Wil] W.BADEN1> That was 1988. And the operative word is "required."

    What is the difference between FOR/NEXT and DO/LOOP? Don't they do the
    same thing?

    -spc

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to sean@conman.org on Sat Jun 28 21:01:48 2025
    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.

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2023 proceedings: http://www.euroforth.org/ef23/papers/
    EuroForth 2024 proceedings: http://www.euroforth.org/ef24/papers/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to Anton Ertl on Thu Jul 3 19:33:30 2025
    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

    --

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)