I have a simple type
data Day = Monday | Tuesday | Wednesday | Thursday | Friday
I'm a newbie in haskell, so I write ==
as follows.
(==) :: Day -> Day -> Bool
Monday == Monday = True
Tuesday == Tuesday = True
Wednesday == Wednesday = True
...
x == y = False
Is there any shorter way to write ==
realization?
You can get the compiler to autogenerate these by using the deriving
keyword:
data Day = Monday | Tuesday | Wednesday | Thursday | Friday
deriving Eq
This will define both ==
and /=
for your datatype.
"Eq may be derived for any datatype whose constituents are also instances of Eq." http://www.haskell.org/ghc/docs/7.4.2/html/libraries/base/Data-Eq.html
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