Function composition and currying

Wolfgang Jeltsch wolfgang@jeltsch.net
Thu, 17 Jul 2003 08:20:24 +0200


Hi,

you may use (f .) . g.

Wolfgang


On Thursday, 2003-07-17, 02:27, CEST, Dr Mark H Phillips wrote:
> Hi,
>
> Hopefully this is a simple question.  I am wanting to know good ways
> of using ".", the function composition operator, when dealing with
> currying functions.
>
> Suppose I have the following functions defined:
>
>   f :: Int -> Int
>   f x = x*x
>
>   g :: Int -> Int -> Int
>   g a b = a + b
>
> If I wish to add 1 and 2 together and then square them I can do:
>
>   f (g 1 2) = 9
>
> but what if I wish to use function composition in the process?
>
> I can't do
>
>   (f.g) 1 2
>
> because the 2 doesn't get passed in till too late.
>
> I could do
>
>   (f.(g 1)) 2
>
> or even
>
>   (f.(uncurry g)) (1,2)
>
> But what I really want is a function with signature Int -> Int -> Int.
> The answer is probably:
>
>   (curry (f.(uncurry g))) 1 2
>
> but this seems awfully messy just to do f (g 1 2).
>
> And what if g were a function with three curried arguments?  Then
> uncurry and curry wouldn't apply.  What then?
>
> Is there a better way?
>
> Thanks,
>
> Mark.