在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会返回一个整数。那么,如何才能将“ Sun”作为返回值,例如wenn cntDays返回“ 6”呢?
问题是cntDays
您退还Integer
一会儿toEnum
只接受Ints
toEnum :: Enum a => Int -> a
您需要转换Integer
为Int
。用途fromIntegral
:
weekday :: Date -> Weekdays
weekday date = toEnum . fromIntegral $ (cntDays date) `mod` 7
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句