I am learning Haskell and I don’t understand why I can do this:
f :: [Int] -> Bool
f l
| l==l = True
| otherwise = False
But I can’t do this.
f :: [a] -> Bool
f l
| l==l = True
| otherwise = False
What’s going on under the hood?
Given two values of an arbitrary type, Haskell does not necessarily know how to compare them for equality.
==is only defined for types that are part of theEqclass.For example, determining if two functions are equal is undecidable in general (I think).
You can compare two lists by checking if each element is equal to its corresponding element in the other list. However, this only makes sense if you can compare the elements for equality, so you have to add a constraint: