0

我一直想知道为什么我不断收到错误:

*** 例外:Prelude.read:没有解析。

这发生在我浏览我的代码并选择选项 2 之后。

这是定义电影

type Title      = String
type Director   = String
type Year       = Int
type Rating     = (String, Int)

data Film = Film Title Director Year [(Rating)] 
        deriving (Eq, Ord, Show, Read)

这是文本文件的示例。

[Film "Blade Runner" "Ridley Scott" 1982 [("Amy",5), ("Bill",8), ("Ian",7), ("Kevin",9), ("Emma",4), ("Sam",7), ("Megan",4)],
 Film "The Fly" "David Cronenberg" 1986 [("Megan",4), ("Fred",7), ("Chris",5), ("Ian",0), ("Amy",6)]]

这是选项 2 功能。

allFilms :: [Film] -> String
allFilms [] = []
allFilms ((Film title director year ratings):films) = "\n Title: " ++ title ++ "\n Director: " ++ director ++ "\n Year: " ++ show year ++ "\n Rating: " ++ printf "%3.2f" (averageRating ratings) ++ "\n" ++ allFilms films

这是到目前为止的 UI 代码:

main :: IO ()
main = do 
contents <- readFile "films.txt"
let database = (read contents :: [Film])
putStrLn "-----------------------------"
putStrLn "Welcome to the Movie Database"
putStrLn "-----------------------------"
putStrLn"Please Enter Your Name: "
user <- getLine
putStrLn user
menu database user

menu :: [Film] -> String -> IO()
menu database user = do
putStrLn "This is the Main Menu"
putStrLn "Choose one of the following options:"
putStrLn "2. give all films in the database"
putStrLn "Your Option:"
functionSelected <- getLine
if (read functionSelected :: Int) > 0 && (read functionSelected :: Int) < 9 then do
    putStrLn ("You chose option: " ++ functionSelected)
    completeFunction functionSelected database user
else do
    putStrLn "Incorrect option restarting menu."


completeFunction :: String -> [Film] -> String -> IO()
completeFunction "2" database user = do
putStrLn "Option 2: "
putStrLn (allFilms database) 

我知道不正确的选项重新启动菜单还不起作用。

4

0 回答 0