簡體   English   中英

Haskell如何將IO元組與普通元組進行比較

[英]Haskell How to compare IO tuple with normal tuple

我嘗試將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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM