你能否告诉我为什么我有错误'无法将预期类型IO t与推断类型字符串匹配' - 请参阅下面的内容以查看错误行:
data RectangleType = Rectangle Int Int Int deriving(Show)
menuRectangles :: [RectangleType] -> IO [RectangleType]
menuRectangles rs = do
putStrLn "Please choose option:"
putStrLn "3 - Show all rectangles"
putStrLn "4 - Quit"
putStr "Number: "
n <- getLine
case n of
"3" -> do { showRectangles rs; menuRectangles rs } -- this line is wrong
"4" -> do { putStrLn "Quitting"; return rs }
otherwise -> do { putStrLn "The End"; return rs }
showRectangles :: [RectangleType] -> String
showRectangles x = showingRectangles x
showingRectangles [] = "Empty"
showingRectangles (x:xs) = do printRectangle x
showingRectangles xs
printRectangle :: RectangleType -> String
printRectangle (Rectangle id width height) = "id: " ++ show id ++ "width: " ++ show width ++ "height: " ++ show height ++ "\n";
答案 0 :(得分:5)
答案 1 :(得分:1)
从IO中获取更多信息,以便了解do块中发生了什么:
data RectangleType = Rectangle Int Int Int deriving (Show)
menuRectangles :: [RectangleType] -> IO [RectangleType]
menuRectangles rs = do
putStr options
n <- getLine
case n of
"3" -> putStrLn (showRectangles rs) >> menuRectangles rs
"4" -> putStrLn "Quitting" >> return rs
otherwise -> putStrLn "The End" >> return rs
showRectangles :: [RectangleType] -> String
showRectangles [] = "Empty"
showRectangles xs = concat (map showRectangle xs)
showRectangle :: RectangleType -> String
showRectangle (Rectangle id width height) =
"id: " ++ show id ++
" width: " ++ show width ++
" height: " ++ show height ++ "\n"
options :: String
options = "Please choose option:\n" ++
"3 - Show all rectangles\n" ++
"4 - Quit\n" ++
"Number "
-- for testing:
r n = Rectangle n n n
rectangleList = map r [1..10]
testlist = showRectangles rectangleList
testempty = showRectangles []
testio = menuRectangles rectangleList