简体   繁体   English

haskell 枚举 - 如果值构造函数需要值而不是空值,该怎么办? 给出需求场景

[英]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:我的问题是:

  1. Where to store Max Days value for each month?在哪里存储每个月的 Max Days 值?
  2. 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?如果是闰年,如何提及和控制二月份的月份,那么二月份的 maxDays 是 29 天,否则它将是 28 天?

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.

相关问题 在 Haskell 中使用值构造函数 - Working with value constructors in Haskell Java中的Haskell样式值构造函数 - Haskell style value constructors in Java Haskell(数据)构造函数构造什么? - What do Haskell (data) constructors construct? 为什么在值构造函数中声明的类型不是Haskell中的类型? - Why are types declared in value constructors not types in Haskell? 理解Haskell中的键值构造函数 - Understanding key-value constructors in Haskell Haskell“id”函数必须返回与传入的值相同的要求的类别 - 理论基础是什么? - What is the category-theoretical basis for the requirement that the Haskell “id” function must return the same value as passed in? Haskell中的Nullary函数的评估 - Evaluation of nullary functions in Haskell 仅具有零元或一元构造函数的 Haskell 数据类型是否被视为代数数据类型? - Are Haskell data types with only nullary or unary constructors considered as Algebraic Data Types? Haskell - 使用 foldl 或 foldr 而不是模式匹配来更新具有给定索引处的新值的列表 - Haskell - using foldl or foldr instead of pattern matching for updating a list with a new value at a given index 在Haskell中的case构造中使用匹配的值 - Use matched value in case construct in Haskell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM