Reputation: 896
What's wrong with this? I tried to write an ordering function defined as this:
let orderFunction:: Num b => (a, [b]) -> (a, [b]) -> Ordering;
orderFunction a1 a2 = if sum $ snd a1 > sum $ snd a2 then GT else LT
but I get an error:
Could not deduce (Ord b) arising from a use of `>'
from the context (Num b)
bound by the type signature for
orderFunction :: Num b => (a, [b]) -> (a, [b]) -> Ordering
at <interactive>:110:21-61
Possible fix:
add (Ord b) to the context of
the type signature for
orderFunction :: Num b => (a, [b]) -> (a, [b]) -> Ordering
In the expression: sum (snd a1) > sum (snd a2)
In the expression: if sum (snd a1) > sum (snd a2) then GT else LT
In an equation for `orderFunction':
orderFunction a1 a2
= if sum (snd a1) > sum (snd a2) then GT else LT
Is there a more Haskell oriented way to write the function?
Thanks, FB
Upvotes: 1
Views: 901
Reputation: 896
As pointed by @ymonad, Num typeclass does not imply that ordering feature is supported. So I added another constraint to function definition as follows:
let orderFunction::(Ord b, Num b) => (a, [b]) -> (a, [b]) -> Ordering;
orderFunction a1 a2 = if sum (snd a1) > sum (snd a2) then GT else LT
Upvotes: 3