用户给出:年,月,日,小时和分钟,我想使用这些值返回CalendarTime值。我该怎么做 - 因为我有错误:'无法匹配预期类型IO CalendarTime与推断类型Int'。函数getDateTime有什么错误?返回日历时间值
命题:所有的值都是正确的,例如月份是从1-12范围内等等 - 我从下面的代码删除验证,否则代码会太长。
isInteger i = not (null i) && all isDigit i
getInteger :: String -> IO Int
getInteger q = do
putStr q;
i <- getLine
if isInteger i == False then do
putStrLn "Bad number"
getInt q
else return (read i)
getDateTime :: String -> IO CalendarTime
getDateTime question = do
putStr question;
year <- getInteger "Year: "
month <- getInteger "Month: "
day <- getInteger "Day: "
hour <- getInteger "Hour: "
minute <- getInteger "Minute: "
return CalendarTime(year month day hour minute 0)
风格点:不要说“if foo == False then ....”;说“如果不是,那么......”。在这种情况下,它将是“如果不是$ isInteger然后...” – 2011-04-20 16:21:34