Rainer Weikusat <rweikusat@???> writes:
> Hendrik Boom <hendrik@???> writes:
>> On Tue, May 24, 2016 at 08:40:37PM +0100, Rainer Weikusat wrote:
>
> [same as below but worded differently]
>
>>>
>>> ... and what the Lisp 1.5 FUNCTION was about was to enable solving the
>>> so-called 'upward funarg problem': Assuming a function is returned
>>> (passed upward) when evaluating an expression and later activated in a
>>> different context, what are free variables used by the returned function
>>> supposed to refer to, the values they had at the time when the function
>>> was defined or the values they had when the function is activated? Eg,
>>> assuming this code,
>>
>> And by wrapping FUNCTION around every lambda-expression (which I did in
>> those days) you oachieve lexical scoping.
>
> By doing that, one achieves capturing the current, dynamic environment,
> IOW, turning the returned function into a closure. But that's (as I
> already wrote) something different from 'lexical scoping'.
[please see original for more detailed explanation]
A quickly done example implementation (in Perl) how to create closures
in an entirely dynamically scoped environment utilizling a global
association list to associate names with values:
-----------------------------------
our $a_list;
sub bind_var
{
$a_list = [[@_], $a_list];
}
sub get_var
{
my ($cur, $pair);
$cur = $a_list;
while ($cur) {
$pair = $cur->[0];
return $pair->[1] if $_[0] eq $pair->[0];
$cur = $cur->[1];
}
return;
}
sub call
{
local $a_list = $a_list;
my $fnc;
$fnc = shift;
if (ref($fnc) eq 'ARRAY') {
$a_list = $fnc->[0];
$fnc = $fnc->[1];
}
return $fnc->(@_);
}
sub function
{
return [$a_list, $_[0]];
}
sub xp1
{
return 1 + get_var('x');
}
sub cxp1
{
call(\&xp1);
}
sub one_more
{
bind_var('x', $_[0]);
return call(\&function, \&cxp1);
}
bind_var('x', 15);
my $closure = call(\&one_more, 5);
print(call(\&cxp1), "\n");
print(call($closure), "\n");
---------------------------------------------------
NB: This reflects my understaning how the LISP 1.5 implementation
probably worked in this respect. That's something I consider interesting
because it's about "programming". I have no idea onto which 'historical'
minefield I stepped this time.