[GAP Forum] Lexical closures

Luca Giuzzi luca.giuzzi at gmail.com
Thu Apr 16 23:19:16 BST 2015


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



More information about the Forum mailing list