Could You tell my why I have error ‘Couldn’t match expected type IO t against inferred type String’ – see below to see bad line:
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";
showingRectanglesis a pure function returning a string. Why use adonotation here? Change it toprintRectangle x ++ showingRectangles xs.Since
showRectanglesis just a pure function, it won’t print to the console. The “statement”showRectangles rsis therefore invalid. You need toputStrLnit.(BTW with this simple fix, the
showingRectangleswill always showEmptyin the last line. You need to add one more definition to the function to avoid this.)