我正在尝试制作一个以日期格式提供日期的辅助函数,所以我有以下代码
localDay :: Day
localDay = do
now <- liftIO $ getCurrentTime
utctDay now
但是得到以下错误
Couldn't match type ‘m0 b0’ with ‘Day’
Expected type: m0 UTCTime -> (UTCTime -> m0 b0) -> Day
Actual type: m0 UTCTime -> (UTCTime -> m0 b0) -> m0 b0
In a stmt of a 'do' block: now <- liftIO $ getCurrentTime
In the expression:
do { now <- liftIO $ getCurrentTime;
utctDay now }
In an equation for ‘localDay’:
localDay
= do { now <- liftIO $ getCurrentTime;
utctDay now }
在这种情况下,我真的无法理解错误的原因,因为现在应该是UTCTimẹ
类型
答案 0 :(得分:2)
getCurrentTime :: IO UTCTime
,因此您不能简单地返回Day
值;您只能返回IO Day
值(忽略unsafePerformIO
)
localDay :: IO Day
localDay = fmap utctDay getCurrentTime