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?
equals() method. A simple solution to compare two lists of primitive types for equality is using the List. equals() method. It returns true if both lists have the same size, and all corresponding pairs of elements in both lists are equal.
Equals(Object) Method which is inherited from the Object class is used to check if a specified List<T> object is equal to another List<T> object or not. Syntax: public virtual bool Equals (object obj);
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 the Eq
class.
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:
f :: Eq a => [a] -> Bool
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With