Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #14014
| From | Bernd Paysan <bernd.paysan@gmx.de> |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: Quotations revisited |
| Date | 2012-07-15 17:08 +0200 |
| Organization | 1&1 Internet AG |
| Message-ID | <jtumec$7a1$1@online.de> (permalink) |
| References | <86839188968435@frunobulax.edu> <91951988968435@frunobulax.edu> |
Marcel Hendrix wrote:
> mhx@iae.nl (Marcel Hendrix) wrote Re: Quotations revisited
> [..]
>> When [: and ;] switch to some private wordlist, EXIT and RECURSE can
>> be used instead of MY-EXIT and MYSELF. LOCALs are possible with a
>> little extra effort. Was there anything else people wanted?
>
> Although a private set of locals doesn't work, accessing outer locals
> may work sufficiently close to what is intended:
In Gforth, you can rather have a private set of locals, no access to
outer locals (i.e. no closures).
> However, Knuth's Man-or-Boy test fails for calldepths > 3. (At that
> point the locals stack is empty, so apparently the recursion is
> supposed to make copies?)
It's supposed to create a closure. A closure contains both the function
itself *and* the values of the locals at the time of its creation. The
livetime of a closure can exceed the livetime of its creating word:
: incrementer ( by-n -- xt ) { by-n } [: by-n + ;] ;
5 incrementer Alias 5+
If I would implement closures in Forth, I'd rather prefer a more
explicit construction that lexical scoping. My closures would look
like:
: incrementer ( by-n -- xt ) [:{ by-n } by-n + ;] ;
What [:{ does at runtime: It allocates a buffer for a structure similar
to words defined by create does>, and the space for by-n, which is
initialized from the stack.
--
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://bernd-paysan.de/
Back to comp.lang.forth | Previous | Next — Previous in thread | Find similar
Re: Auotations revisited Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-07-15 03:45 -0500
Re: Quotations revisited mhx@iae.nl (Marcel Hendrix) - 2012-07-15 12:52 +0200
Re: Quotations revisited mhx@iae.nl (Marcel Hendrix) - 2012-07-15 16:08 +0200
Re: Quotations revisited Bernd Paysan <bernd.paysan@gmx.de> - 2012-07-15 17:08 +0200
csiph-web