Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short == realization

Tags:

haskell

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?

like image 634
Nelson Tatius Avatar asked Dec 02 '22 21:12

Nelson Tatius


1 Answers

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

like image 156
Deestan Avatar answered Dec 14 '22 23:12

Deestan