haskell电影数据库没有解析

时间:2016-03-10 13:45:08

标签: database haskell exception movie haskell-prelude

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

  

***异常: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) 

我知道错误的选项重启菜单不起作用。

0 个答案:

没有答案