我是Haskell的新手,我需要一些帮助。我想创建一个函数,以yyyy,mm,dd格式接收用户输入,并将其转换为Day类型,以便使用diffDays。
这是我尝试过的:
today :: IO (Integer, Int, Int)
today = getCurrentTime >>= return . toGregorian . utctDay
getDiffDays' :: IO Integer
getDiffDays' = do
putStr "What is your birthdate (year, month, day)?: "
dateOfBirth <- getLine
let dob = read dateOfBirth :: Day
return diffDays today dob
答案 0 :(得分:-2)
我想你想要下面的东西,
import Data.Time.Calender
import Data.List (intercalate)
replace :: String -> String
replace src = intercalate "-" $ words $ filter (/= ',') src
main :: IO ()
main = do
-- Example input: 2018, 11, 12
dayString <- getLine
let day = read (replace dayString) :: Day
-- Example output: 2018-11-12
print day
问题是Day
期望读取格式为YYYY-MM-DD
,但YYYY, MM, DD