While looking for Polyvariadic function examples, I found this resource: StackOverflow: How to create a polyvariadic haskell function?, and there was an answer snippet like this:
class SumRes r where
sumOf :: Integer -> r
instance SumRes Integer where
sumOf = id
instance (Integral a, SumRes r) => SumRes (a -> r) where
sumOf x = sumOf . (x +) . toInteger
Then we could use:
*Main> sumOf 1 :: Integer
1
*Main> sumOf 1 4 7 10 :: Integer
22
*Main> sumOf 1 4 7 10 0 0 :: Integer
22
*Main> sumOf 1 4 7 10 2 5 8 22 :: Integer
59
I tried out to change it a little bit, just for curiosity, because I found it pretty tricky at first glance, and I got into this:
class SumRes r where
sumOf :: Int -> r
instance SumRes Int where
sumOf = id
instance (SumRes r) => SumRes (Int -> r) where
sumOf x = sumOf . (x +)
I just changed Integer
to Int
and turned instance (Integral a, SumRes r) => SumRes (a -> r) where
less polymorphic to instance (SumRes r) => SumRes (Int -> r) where
To compile it I had to set XFlexibleInstances
flag. When I tryed to test sumOf
function I got a problem:
*Main> sumOf 1 :: Int
1
*Main> sumOf 1 1 :: Int
<interactive>:9:9
No instance for (Num a0) arising from the literal `1'
The type variable `a0' is ambiguous...
Then I tried:
*Main> sumOf (1 :: Int) (1 :: Int) :: Int
2
Why can't Haskell infer that we want an Int
in this situation, considering we are using Int
within our SumRes
typeclass?