Sujet : Re: Back & Forth - Co-routines
De : albert (at) *nospam* spenarnc.xs4all.nl
Groupes : comp.lang.forthDate : 10. Feb 2025, 14:40:05
Autres entêtes
Organisation : KPN B.V.
Message-ID : <nnd$4b224adf$7ef7b039@cbdfa791fed07730>
References : 1 2 3 4
User-Agent : trn 4.0-test77 (Sep 1, 2010)
In article <
2025Feb10.081905@mips.complang.tuwien.ac.at>,
Anton Ertl <
anton@mips.complang.tuwien.ac.at> wrote:
melahi_ahmed@yahoo.fr (ahmed) writes:
On Sun, 9 Feb 2025 23:08:22 +0000, Anton Ertl wrote:
>
Paul Rubin <no.email@nospam.invalid> writes:
..
This is an attempt to make a counting function, like in Scheme:
>
(define (x)
((lambda (n)
(lambda ()
(set! n (+ 1 n))
n)) 0))
>
(define a (x))
...
: x ( -- xt )
here 0 , [{: addr :}d addr @ 1+ dup addr ! ;] ;
>
x alias a
...
: ctr: create 0 , does> dup @ 1+ dup rot ! ; ok
ctr: a
...
So, what is the difference between the two definitions?
>
One produces an xt, the other a named word; the latter is more
convenient for the shown usage).
>
But yes, for dictionary allocation Forth has had a way to associate
data with a single action since very early on.
Make that
"
Forth has had a way to associate
data with a single action since very early on.
"
or more precise
" Forth recipes combine code and data"
Using underlying more primitive facilities in a Forth, one can define
a recipe ("xt") that has no name, is not linked in a wordlist and sits
in ALLOCATEd space and perform an action like x.
(Admittedly, in a Forth that separates code and data it is more involved)
This is a simple example to get a word `test floating in allocated space.
(In ciforth example)
ALLOC moves a word to the heap freeing the a freshly generated object
that runs from ( addr --) to HERE. This doesn't switch DP, and assumes
relocatable code.
UNLINK-LATEST decouples the latest word from the dictionary.
WANT >ALLOC UNLINK-LATEST
INIT-ALLOC \ Use 1/4 of the dictionary space for heap.
: test "hello" TYPE ;
' test UNLINK-LATEST \ decouple
ALLOC \ move to heap
WORDS \ to show that test has vanished
.S \ The xt
EXECUTE
hello OK
In the above example reclaiming memory is not harder than with
any dynamically allocated memory.
[This doesn't show how the data is coupled, but conveys hopefully
a part of the idea.]
<SNIP>
But, as mentioned below, the textbook examples of changing data in
closures or DOES> words are rarely found in practice.
I totally agree on this. In 400+ euler problems I was never inclined
to use this technique (if this counts for something ..)
<SNIP>
- anton
-- Temu exploits Christians: (Disclaimer, only 10 apostles)Last Supper Acrylic Suncatcher - 15Cm Round Stained Glass- Style WallArt For Home, Office And Garden Decor - Perfect For Windows, Bars,And Gifts For Friends Family And Colleagues.