我尝试将 IO 元组的元组成员(日期(与普通元组进行比较。
d1 ->(Integer, Int, Int)
和d2 -> IO (Integer, Int, Int)
,
是否可以比较这两个元组?我试过这样的事情:
import Data.Time.Clock
import Data.Time.Calendar
import Data.Time.LocalTime
-- helper functions for tuples
x_fst (x,_,_) = x
x_snd (_,x,_) = x
x_trd (_,_,x) = x
getDate :: IO (Integer, Int, Int)
getDate = do
now <- getCurrentTime
tiz <- getCurrentTimeZone
let zoneNow = utcToLocalTime tiz now
let date@(year, month, day) = toGeorgian $ localDay zoneNow
return $ date -- here I will return an IO tuple -> IO (Integer, Int, Int)
compareDates :: a -> IO (Integer, Int, Int) -> IO Bool
compareDates d1 d2 = do
let year1 = x_fst d1
let year2 = x_fst d2
let month1 = x_snd d1
let month2 = x_snd d2
let day1 = x_trd d1
let day2 = x_trd d2
return $ (year1 == year2 && month1 == month2 && day1 == day2)
但是我得到的消息是我无法将 IO 元组与普通元组进行比较:
Couldn't match expected type `(Integer, Integer, Integer)`
with actual type `IO (Integer, Int, Int)`
In the second argument of `compareDates`, namely `date`
有没有办法解决它?我将不胜感激任何帮助。
谢谢。
在评论/聊天部分的帮助下,我让它使用以下代码:
getDate :: IO Day
getDate = do
now <- getCurrentTime
tz <- getCurrentTimeZone
return . localDay $ utcToLocalTime tz now
main = do
d2 <- getDate
return $ fromGregorian 2019 6 15 == d2