Novice question. Let's say that I've created a simple list typeclass that takes two strings and adds their respective elements if they're integers or concatenates them if they're strings:
class NewList a where
addLists :: [a] -> [a] -> [a]
instance NewList Int where
addLists = addNumLists
addNumLists :: [Int] -> [Int] -> [Int]
addNumLists (x : xs) (y : ys) = x + y : addNumLists xs ys
addNumLists _ _ = []
instance NewList Char where
addLists x y = concat [x,y]
This compiles, but if I run addLists [1,2,3] [1,2,3] in GHCi I get the error
<interactive>:278:11:
Ambiguous type variable `a0' in the constraints:
(Num a0) arising from the literal `1' at <interactive>:278:11
(NewList a0)
arising from a use of `addLists' at <interactive>:278:1-8
Probable fix: add a type signature that fixes these type variable(s)
In the expression: 1
In the first argument of `addLists', namely `[1, 2, 3]'
In the expression: addLists [1, 2, 3] [1, 2, 3]
Adding :: [Int] to the expression allows it to be evaluated, but I don't understand why the error appears in the first place.