简体   繁体   中英

Haskell How to compare IO tuple with normal tuple

I try to compare the tuple members (date) of an IO tuple with a normal tuple.

d1 ->(Integer, Int, Int) and d2 -> IO (Integer, Int, Int) ,

Is it possible to compare these two tuples? I've tried something like that:

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)

But I get the message that I can't compare an IO tuple with a normal tuple:

Couldn't match expected type `(Integer, Integer, Integer)` 
  with actual type `IO (Integer, Int, Int)`
  In the second argument of `compareDates`, namely `date`

Is there a way around it? I would appreciate any help.

Thanks.

With the help of the comment / chat section I got it to work with the following code:

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

main = do
    d2 <- getDate
    return $ fromGregorian 2019 6 15 == d2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM