[英]haskell enum - what to do in case value constructors require value instead of nullary? Requirement scenario is given
LYAH says at Derived Instances that LYAH在派生实例中说
[...] all the value constructors are nullary (take no parameters, ie fields), we can make it part of the Enum typeclass.
[...] 所有的值构造器都是空的(不带参数,即字段),我们可以使它成为 Enum 类型类的一部分。
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:我的问题是:
In Java one can code like given below:在 Java 中,可以编写如下所示的代码:
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;
}
This should work.这应该有效。
data Month = January | February | March | April | May
| June | July | August |September | October
| November | December
deriving (Eq, Ord, Show, Read, Bounded, Enum)
type Year = Int
isLeapYear :: Year -> Bool
isLeapYear year = year `mod` 4 == 0 && (year `mod` 100 /= 0 || year `mod` 400 == 0)
getMaxDays :: Year -> Month -> Int
getMaxDays _ January = 31
getMaxDays year February
| isLeapYear year = 29
| otherwise = 28
getMaxDays _ March = 31
getMaxDays _ April = 30
getMaxDays _ May = 31
getMaxDays _ June = 30
getMaxDays _ July = 31
getMaxDays _ August = 31
getMaxDays _ September = 30
getMaxDays _ October = 31
getMaxDays _ November = 30
getMaxDays _ December = 31
Why do you need Month
to be an Enum?为什么需要
Month
成为 Enum? It seems to me that you are trying to force an OO style in your code, which is not a good idea.在我看来,您试图在代码中强制使用 OO 风格,这不是一个好主意。 The Java object oriented style of writing code does not cleanly translate to functional languages like Haskell.
Java 面向对象的代码编写风格并没有完全转换为像 Haskell 这样的函数式语言。
Where in OO, you would bundle the data structure and all associated operations on that data in a class, in FP, you would define the data structure separately from the associated operations.在 OO 中,您会将数据结构和对该数据的所有关联操作捆绑在一个类中,而在 FP 中,您将定义数据结构与关联操作分开。 This means that the FP approach makes it easier to define new operations on data, where as OO approach makes it easier to add new information to your data structure.
这意味着 FP 方法可以更轻松地定义对数据的新操作,而 OO 方法则可以更轻松地向数据结构添加新信息。 Personally I find myself defining new operations a lot more than adding new fields and FP style suits that well.
就我个人而言,我发现自己定义新操作比添加新字段和 FP 风格套装要多得多。
The closest analogue to the Java example in Haskell would be to define a Typeclass -最接近 Haskell 中的 Java 示例的是定义一个 Typeclass -
data Month = January | February | March | April
| May | June | July | August |September
| October | November |December
deriving (Eq, Ord, Show, Read, Bounded, Enum)
data Year = Int
class HasDays X where
maxdays :: X -> Int
days :: X -> Year -> Int
-- Any other "methods" here
instance HasDays Month where
maxdays January = 31
maxdays February = 29
maxdays .. = .. -- Similar code for other months
days February y = .. -- Leap year calculation
days m _ = maxdays m
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.