[haskell-llvm] Beginner - Anonymous Functions

Henning Thielemann lemming at henning-thielemann.de
Fri Nov 18 17:04:00 GMT 2011


On Fri, 18 Nov 2011, Elliott Pace wrote:

> I'd like to have "curried" functions. To do this, I need to track previously entered
> arguments to a function.

I would try the "object oriented" approach: I would represent a function 
by an OOP object, that consists of member variables and a method that 
computes the actual function using the member variables. The member 
variables store the previously given arguments.


// function with one argument
class function1 {
    virtual int apply (int x);
};

// curried function with two arguments
class function2 {
    virtual function1 *apply (int x);
};


class plus1 : function1 {
    plus1(int x) { this->x = x };
    int x;
    virtual int apply (int y) { return x+y };
}

class plus2 : function2 {
    virtual function1 *apply (int x) { return new plus1(x) };
}


Now, an object of class plus1 is isomorphic to

struct plus1 {
    int (*apply) (int y);
    int x;
}

and an object of class plus2 is isomorphic to

struct plus2 {
    plus1 * (*apply) (int x);
}


Since all sub-classes of function1 start like

struct subclass1 {
    int (*apply) (int y);
    ...
    // member variables follow
}

the apply method can be called independent of the type and number of 
member variables.



More information about the Haskell-llvm mailing list