Bill Allombert on Tue, 30 Oct 2007 12:44:47 +0100


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

Re: Functions as first-class objects


On Thu, Oct 11, 2007 at 01:55:16AM +0200, Bill Allombert wrote:
> Hello PARI-dev,
> 
> I would like to discuss moving user functions to first-class objects,
> and add support for anonymous functions and closures.

I have commited an improved version of that patch.

The patch adds the following syntax:

 * (x1,...,xn)->EXPR : create an anonymous function.
 * x1->EXPR is accepted as a short-hand for (x1)->EXPR
 * f(x1,...,xn)=EXPR is accepted as a short-hand for f=(x1,...,xn)->EXPR
 * EXPR(x1,...,xn) evaluate the expression EXPR. If the result is a
   function, it calls it on (x1,...,xn), else it fails.

Incompatibilities:
 * Calling a function without () no more evaluate it:
? f
%2 = (x) -> x^4+1
 * f(x)=x^4+1 is equivalent to f=(x)->x^4+1 which return the 'value'
(x)->x^4+1 instead of void:
? f(x)=x^4+1
%1 = (x) -> x^4+1

Deficiencies:

 * It is not possible to define recursive anonymous functions (short
of the Y combinator). Maybe we need to add a 'self' construction.

Cheers,
Bill.