Path: ...!weretis.net!feeder9.news.weretis.net!i2pn.org!i2pn2.org!.POSTED!not-for-mail From: minforth@gmx.net (minforth) Newsgroups: comp.lang.forth Subject: Re: Back & Forth - Co-routines Date: Sun, 9 Feb 2025 19:15:29 +0000 Organization: novaBBS Message-ID: <914f1d7f6d3e010799fec2544312f72f@www.novabbs.com> References: <874j1aycdt.fsf@nightsong.com> <3c3bdb056696f15c43fa512b5366002d@www.novabbs.com> <2025Feb6.135712@mips.complang.tuwien.ac.at> <3955434636b2a293c6a9c6d726ff6eae@www.novabbs.com> <2025Feb6.180659@mips.complang.tuwien.ac.at> <2cf9bd53562dae1241a161036e87cd6b@www.novabbs.com> <2025Feb8.120627@mips.complang.tuwien.ac.at> <87ed07vw8e.fsf@nightsong.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: i2pn2.org; logging-data="3467910"; mail-complaints-to="usenet@i2pn2.org"; posting-account="BZ29EnRIXfdX99fE+pfHgueF2xuF1KvSpLJsfuOZy3Y"; User-Agent: Rocksolid Light X-Rslight-Posting-User: 0338a3da6f3e9c9f1401b365bcd9c3ed8de2227a X-Spam-Checker-Version: SpamAssassin 4.0.0 X-Rslight-Site: $2y$10$2.DDxqrvUTrj0F0AagXPIOPzxVSaY0kOYhzphmgIp7xt9hRSMvz.y Bytes: 2799 Lines: 47 On Sun, 9 Feb 2025 17:55:29 +0000, Paul Rubin wrote: > anton@mips.complang.tuwien.ac.at (Anton Ertl) writes: >> The :}D means that the closure data is stored in the dictionary; there >> is also :}L (for locals, deallocated when the surrounding definition >> is exited), :}H (heap, deallocated with FREE-CLOSURE), and :}H1 (heap, >> deallocated right after the first (and only) execution). > > This is pretty cool, but it looks like quotations within the closure > aren't allowed to access the closure's locals, using them as OOP-like > state. In the current Gforth git snapshot: > > : x [{: n :}d [: n 1+ dup to n ;] ;]h 0 execute ; > > gives: > > *the terminal*:26:30: error: Unsupported operation > : x [{: n :}d [: n 1+ dup to >>>n<<< ;] ;]h 0 execute ; > > 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)) > > (a) ; 1 > (a) ; 2, etc. FWIW a single quotation-based counter in another Forth: MinForth 3.6 (32 bit) # defer ctr ok # : init { n } [: n 1+ dup to n ;] ; ok # 4 init is ctr ok # ctr . 5 ok # ctr . 6 ok # ctr . 7 ok # Generalisation would of course require closures and memory management after use. IOW read/write access to locals of the parent function opens up new possiblities in Forth - perhaps also an idea for gforth.