不知道如何在Haskell中使用toEnum

时间:2015-10-24 15:21:20

标签: haskell

在haskell中我是新手。我有以下代码:

data Weekdays = Mon | Tue | Wed | Thu | Fri | Sat | Sun
                deriving (Eq, Enum, Show)

weekday :: Date -> Weekdays
weekday date = toEnum (mod (cntDays date) 7)

我收到以下错误消息:

Time.hs:8:29:
    Couldn't match expected type ‘Int’ with actual type ‘Integer’
    In the first argument of ‘mod’, namely ‘(cntDays date)’
    In the first argument of ‘toEnum’, namely ‘(mod (cntDays date) 7)’
Failed, modules loaded: none.

cntDays返回一个整数。那我怎么能回来太阳'例如作为一个返回值wenn cntDays返回一个' 6'?

2 个答案:

答案 0 :(得分:3)

问题是cntDays返回IntegertoEnum只接受Ints

toEnum :: Enum a => Int -> a

您需要将Integer转换为Int。使用fromIntegral

weekday :: Date -> Weekdays
weekday date = toEnum . fromIntegral $ (cntDays date) `mod` 7

答案 1 :(得分:0)

您可以使用fromIntegerInteger变成Int,但最好只使用Int开头。