home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.forth      Forth programmers eat a lot of Bratwurst      117,951 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 117,068 of 117,951   
   albert@spenarnc.xs4all.nl to Anton Ertl   
   Re: Back & Forth - Co-routines   
   10 Feb 25 14:40:05   
   
   In article <2025Feb10.081905@mips.complang.tuwien.ac.at>,   
   Anton Ertl  wrote:   
   >melahi_ahmed@yahoo.fr (ahmed) writes:   
   >>On Sun, 9 Feb 2025 23:08:22 +0000, Anton Ertl wrote:   
   >>   
   >>> Paul Rubin  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.]   
      
      
      
   >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 ..)   
      
      
      
   >- anton   
   --   
   Temu exploits Christians: (Disclaimer, only 10 apostles)   
   Last Supper Acrylic Suncatcher - 15Cm Round Stained Glass- Style Wall   
   Art For Home, Office And Garden Decor - Perfect For Windows, Bars,   
   And Gifts For Friends Family And Colleagues.   
      
   --- SoupGate-DOS v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca