I know that for a type to have an instance of the Num
typeclass, there must be one from Eq
and Show
class (Eq a, Show a) => Num a
I'm wondering why it's required to be Eq
rather than Ord
. Does it make sense for a numerical type to be in Eq
but not in Ord
?
The Ord class is used for totally ordered datatypes. Instances of Ord can be derived for any user-defined datatype whose constituent types are in Ord. The declared order of the constructors in the data declaration determines the ordering in derived Ord instances.
Num is a typeclass — a group of types — which includes all types which are regarded as numbers. The (Num a) => part of the signature restricts a to number types – or, in Haskell terminology, instances of Num .
In Haskell it is just another character to distinguish identifiers and the identifier is then called fold prime , but it is commonly used in the same way as it used in mathematics.
The Nil constructor is an empty list. It contains no objects. So any time you're using the [] expression, you're actually using Nil . Then the second constructor concatenates a single element with another list.
Complex numbers, for example, can be added, subtracted, multiplied and tested for equality, but not ordered. See Complex a
from Data.Complex in base.
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