I’m extremely new to haskell and was trying to implement a small and a simple function that takes two strings and tells me the number of same characters at the same location.
ed :: (Integral b) => [a] -> [a] -> b
ed _ [] = 0
ed [] _ = 0
ed [] [] = 0
ed (x:xs) (y:ys)
| x == y = 1 + ed xs ys
| otherwise = ed xs ys
this doesn’t run because my typeclass definition is wrong. I have two strings and need to return an integer and hence the typeclass definition I have written above. Is there something else I need to do?
The type signature should be
This is because your definition of
edincludes the expressionx == y.xandyboth have typea; to be able to test them for equality, this type must implement theEqtypeclass, which provides the==and/=operators.The error message you got would have included something like this:
which was trying to tell you this.
(Btw, your code doesn’t handle the case when when the strings have different lengths.)