-1

IO タプルのタプル メンバー (日付) を通常のタプルと比較してみます。

d1 ->(Integer, Int, Int)そしてd2 -> IO (Integer, Int, Int)

これら2つのタプルを比較することは可能ですか? 私はそのようなことを試しました:

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`

それを回避する方法はありますか?助けていただければ幸いです。

ありがとう。

4

1 に答える 1

1

コメント/チャットセクションの助けを借りて、次のコードで動作するようになりました:

getDate :: IO Day
getDate = do
    now <- getCurrentTime
    tz <- getCurrentTimeZone
    return . localDay $ utcToLocalTime tz now

main = do
    d2 <- getDate
    return $ fromGregorian 2019 6 15 == d2
于 2019-06-15T18:07:57.440 に答える