Charles Greathouse on Mon, 17 Nov 2014 21:52:03 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: New GP function self() for recursive anonymous functions


I think that self() is very useful and I've wanted it for some time (but unwilling to ask, since there have been so many things to be done). But is it efficient? I see that it gcopies the function.

Charles Greathouse
Analyst/Programmer
Case Western Reserve University

On Mon, Nov 17, 2014 at 12:49 PM, Bill Allombert <Bill.Allombert@math.u-bordeaux.fr> wrote:
Dear PARI developers,

I have added a GP function self() which allow to write recursive anonymous
functions:

? (n->if(n==0,1,n*self()(n-1)))(5)
%1 = 120

self() returns the calling function or closure.

This is useful to avoid using global variables when using parallel GP:
For example
my(f(n)=if(n==0,1,n*self()(n-1))); parapply(f,[100..200])
is valid but
f(n)=if(n==0,1,n*f()(n-1))
parapply(f,[100..200])

is not.

Please tell me if it is useful to you.

Cheers,
Bill.