[GAP Forum] Lexical closures

Stephen Linton steve.linton at st-andrews.ac.uk
Thu Apr 16 23:28:08 BST 2015


Actually that can be shortened to 

MakeF2 := q->x->x+q;


> On 16 Apr 2015, at 23:26, Stephen Linton <steve.linton at st-andrews.ac.uk> wrote:
> 
> This seems rather complicated compared to
> 
> MakeF2 := function(q)
> 	return x-> x+q:
> end;
> 
> Then you can use 
> 
> f0 := MakeF2(10);
> f1 := MakeF2(20);
> 
> Does this do what you need?
> 
> 	Steve
> 
> On 16 Apr 2015, at 23:19, Luca Giuzzi <luca.giuzzi at gmail.com> wrote:
>> 
>> Dear Forum,
>> is there a canonical way in gap to generate lexical closures for functions?
>> The best I could come up with is something like
>> 
>> MakeF:=function ()
>>   local  q;
>>   q := ValueGlobal( "q" );
>>   return function(x)
>>      return x+q;
>>   end;
>> end;
>> 
>> which captures the value of the global value of q, so that
>> q:=10; f0:=MakeF();
>> q:=20  f1:=MakeF();
>> gives
>> f0(0)=10 and f1(0)=20
>> and it "sort of works" (and, at least for the cases I have in mind it
>> might be enough)
>> Still, I am asking if there might a better (or canonical) way to implement this?
>> 
>> Regards,
>> luca
>> 
>> _______________________________________________
>> Forum mailing list
>> Forum at mail.gap-system.org
>> http://mail.gap-system.org/mailman/listinfo/forum
> 
> 
> _______________________________________________
> Forum mailing list
> Forum at mail.gap-system.org
> http://mail.gap-system.org/mailman/listinfo/forum




More information about the Forum mailing list