なぜエラーが発生し続けるのか疑問に思っていました:
*** 例外: 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)
間違ったオプションの再起動メニューがまだ機能していないことは承知しています。