LYAH says at Derived Instances that
[...] all the value constructors are nullary (take no parameters, i.e. fields), we can make it part of the Enum typeclass.
data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday deriving (Eq, Ord, Show, Read, Bounded, Enum)
Now, if I take months, it will be
data month = January | February | March | April | May | June | July | August |September | October | November |December deriving (Eq, Ord, Show, Read, Bounded, Enum)
My questions are:
- Where to store Max Days value for each month?
- How to mention and control that for February month if it is leap year then maxDays for February is 29 days otherwise it will be 28 days?
In Java one can code like given below:
public enum Month {
January (31),
February (29),
March (31),
April (30),
May (31),
June (30),
July (31),
August (31),
September (30),
October (31),
November (30),
December (31),
;
private int maxDays; // instance variables
private (int maxDays) { // constructors always private
this.maxDays = maxDays;
}
Public int getMaxDays () {
return maxDays;
}