I am learning currying and uncurrying of functions in Haskell. If I have this code:
fc :: Num a => a -> a -> a -- curried function
fc x y = x + y
fu :: Num a => (a, a) -> a --uncurried function
fu (x,y) = fc x y
uncurry' :: Num a => (a -> a -> a) -> Num a => (a, a) -> a
uncurry' f(x,y) = f x y
main = print(uncurry' fc 4 5)
The compiler complains with the following error:
"Illegal polymorphic or qualified type: Num a => (a, a) -> a".
What is the correct type signature for uncurry'
?
You only need one constraint on the type signature:
uncurry' :: Num a => (a -> a -> a) -> (a, a) -> a
uncurry' f (x, y) = f x y
You don't have to add the extra constraint in the middle of the type signature, Haskell doesn't like this. All constraints go before the =>
, and only one should appear in the type signature (until you enable certain compiler extensions, but that's beyond the scope of this answer).
However, as jamshidh points out, you can make this signature more general:
uncurry' :: (a -> b -> c) -> (a, b) -> c
and it will still work just fine. Remember that you can always force a signature to go from more general to less general.
Once you uncurry the function, it is type Num a=>(a, a)->a
, so you should give it a tuple as input parameter
main = print(uncurry' fc (4, 5))
I would, however, keep the type of uncurry' more general
(a -> b -> c) -> (a, b) -> c
Once I made this change, it worked for me.
Oh, and @bheklilr noticed something that I didn't even see.... His fix plus mine should get you up and going....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With