reverse function application

Tom Pledger Tom.Pledger@peace.com
Wed, 12 Jun 2002 12:08:27 +1200


Hal Daume III writes:
 :
 | 5 *== \x -> 6 *== \y -> somefunctiononxandy x y
 | 
 | but i'd really like to be able to write:
 | 
 | 5 *== 6 *== somefunctiononxandy
 :
 | any advice?

If you want to do something akin to currying, to eliminate the
explicit lambdas, I think you'll have to clutter up the right hand end
of the expression instead.

The basic problem seems to be that you're trying to match arguments to
parameters in an order which goes against the expression's syntax.
Contrast the following:

    5 *== 6 *== somefunctiononxandy
   arg ---------------------------> param
         arg ---------------------------> param

    5 *== 6 *== somefunctiononYandX
   arg ---------------------------------> param
         arg ---------------------> param

So, there's a way using flip:

    infixr 1 *==
    a *== f = f a

    flip3 f a b c = f c b a

    test f3 = 5 *== 6 *== 7 *== flip3 f3
    -- test (,,) --> (5,6,7)

If you dislike having to use different clutter (flip, flip3, etc.)
depending on the number of (*==)s in the expression, you could make
(*==) a function of 3 parameters instead of 2, and use an argument
accumulator:

    infixr 1 *==
    (a *== f) applyToOtherAs = f (($a) . applyToOtherAs)

    test f2 = 5 *== 6 *== ($f2) $ id
    -- test (,) --> (5,6)
    -- :t (*==) --> left as a strenuous exercise for the reader

Regards,
Tom