Function composition and currying
Dr Mark H Phillips
mark@austrics.com.au
25 Jul 2003 16:51:17 +0930
Thanks to all the people who responded to my question!
The solution from Wolfgang Jeltsch:
(f.).g
was what I was after. But the other responses were
useful also.
Thanks!
Mark.
On Thu, 2003-07-17 at 09:57, 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.
>
>
> --
> Dr Mark H Phillips
> Research Analyst (Mathematician)
>
> AUSTRICS - smarter scheduling solutions - www.austrics.com
>
> Level 2, 50 Pirie Street, Adelaide SA 5000, Australia
> Phone +61 8 8226 9850
> Fax +61 8 8231 4821
> Email mark@austrics.com.au
>
> _______________________________________________
> Haskell mailing list
> Haskell@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
--
Dr Mark H Phillips
Research Analyst (Mathematician)
AUSTRICS - smarter scheduling solutions - www.austrics.com
Level 2, 50 Pirie Street, Adelaide SA 5000, Australia
Phone +61 8 8226 9850
Fax +61 8 8231 4821
Email mark@austrics.com.au