User can give id, width, height and description rectangle and then I write it to a file. Now I would like to load this content from the file to my program but I have error:
Couldn't match expected type [RectangleType] against inferred type IO [Rectangletype]. In the first argument of menuRectangles namely db. In the expression menuRectangles db. In a do expression menuRectangles db.
What is going on ? This is content of my file: [Rectangle 2 5 6 "abcabc",Rectangle 1 2 4 "abcabc"]
This is code:
import IO
import Char
import System.Exit
import Maybe
data RectangleType = Rectangle Int Int Int deriving(Show, Read)
loadFile :: FilePath -> IO [RectangleType]
loadFile fname =
catch (do fileContent <- readFile fname
return (read fileContent)
) errorHandler
where
errorHandler e = do putStrLn ("Error file")
exitFailure
db = loadFile "db.txt"
main = do
putStrLn "Choose option:"
n <- getLine
case n of
"1" -> do menuRectangles db; main
"2" -> putStrLn "bye, bye"
otherwise -> do putStrLn "Bad option"; main
menuRectangles :: [RectangleType] -> IO [RectangleType]
menuRectangles rs = do
putStrLn "Please choose option:"
putStrLn "1 - Add rectangle"
putStrLn "2 - Show rectangle"
putStrLn "3 - Quit"
putStr "Number: "
n <- getLine
case n of
"1" -> do { {- rs_new <- addRectangle rs; -} menuRectangles rs };
"2" -> do { {- showRectangle rs; -} menuRectangles rs }
"3" -> do { putStrLn "Quitting"; return rs }
otherwise -> do { putStrLn "The End"; return rs }
EDIT: correct code:
import IO
import Char
import System.Exit
import Maybe
data RectangleType = Rectangle Int Int Int deriving(Show, Read)
loadFile :: FilePath -> IO [RectangleType]
loadFile fname =
catch (do fileContent <- readFile fname
return (read fileContent)
) errorHandler
where
errorHandler e = do putStrLn ("Error file")
exitFailure
main = do
db <- loadFile "db.txt"
mainMenu db
mainMenu rs = do
putStrLn "Choose option:"
n <- getLine
case n of
"1" -> do menuRectangles rs; mainMenu rs
"2" -> putStrLn "bye, bye"
otherwise -> do putStrLn "Bad option"; mainMenu rs
menuRectangles :: [RectangleType] -> IO [RectangleType]
menuRectangles rs = do
putStrLn "Please choose option:"
putStrLn "1 - Add rectangle"
putStrLn "2 - Show rectangle"
putStrLn "3 - Quit"
putStr "Number: "
n <- getLine
case n of
"1" -> do { {- rs_new <- addRectangle rs; -} menuRectangles rs };
"2" -> do { {- showRectangle rs; -} menuRectangles rs }
"3" -> do { putStrLn "Quitting"; return rs }
otherwise -> do { putStrLn "The End"; return rs }