如何将(Integer,Int,Int)转换为Day。无法将预期类型'Day'与实际类型'(Integer,Int,Int)'匹配

时间:2018-06-03 13:57:45

标签: haskell haskell-stack

main :: IO ()
main = do
  Prelude.putStrLn "Please,enter date YYYY-MM-DD"
  currentTime <- getCurrentTime
  date <- Prelude.getLine
  let sTime = show currentTime 
  let retrievedDate = toGregorian $ utctDay currentTime
  let forecastDay = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date :: Maybe Day
  let diifedDays = diffDays (fromJust forecastDay) retrievedDate
  if date >= show retrievedDate && diifedDays > 0 && diifedDays <= 16 
     then print date
     else print "Time Error!"

我需要retrieveDate(Integer,Int,Int)转换为Day。 任务是:减去(forecastDay - discoveredDay) 但我不能这样做,因为我需要retrieveDate转换为Day

错误讯息:  无法匹配预期类型'Day'                   实际类型'(整数,整数,国际)'

42 |让diifedDays = diffDays(fromJust forecastDay)retrieveDate

2 个答案:

答案 0 :(得分:2)

实际上你已经有了Day个对象,但是使用toGregorian,你可以将它转换为三个(Integer, Int, Int)(年,月,日根据 Gregorian 日历)。所以你实际上可以放弃toGregorian函数调用:

main :: IO ()
main = do
  Prelude.putStrLn "Please,enter date YYYY-MM-DD"
  currentTime <- getCurrentTime
  date <- Prelude.getLine
  let sTime = show currentTime 
  let retrievedDate = utctDay currentTime
  let forecastDay = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date :: Maybe Day
  let diifedDays = diffDays (fromJust forecastDay) retrievedDate
  if date >= show retrievedDate && diifedDays > 0 && diifedDays <= 16 
     then print date
     else print "Time Error!"

如果您需要转换三联,则可以使用fromGregorian :: Integer -> Int -> Int -> Day功能。对于三(Integer, Int, Int),我们可以使用:

\(y, m, d) -> fromGregorian y m d

除此之外,您的代码会产生相当混乱的印象,包含大量let语句,show等等。所以我真的建议您清理它。

答案 1 :(得分:2)

修复错误所需要的只是删除toGregorian,它会很好地解决问题。

你可能想要处理你的逻辑。