在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'?
答案 0 :(得分:3)
问题是cntDays
返回Integer
而toEnum
只接受Ints
toEnum :: Enum a => Int -> a
您需要将Integer
转换为Int
。使用fromIntegral
:
weekday :: Date -> Weekdays
weekday date = toEnum . fromIntegral $ (cntDays date) `mod` 7
答案 1 :(得分:0)
您可以使用fromInteger
将Integer
变成Int
,但最好只使用Int
开头。