这是我当前的代码,我希望能够创建一个充当接口的 MAIN IO,并允许用户根据自己的选择选择要执行的函数。
type Title = String
type Actor = String
type Cast = [Actor]
type Year = Int
type Fan = String
type Fans = [Fan]
type Period = (Year, Year)
type Film = (Title, Cast, Year, Fans)
type Database = [Film]
title (t, _, _, _) = t
cast (_, c, _, _) = c
year (_, _, y, _) = y
fans (_, _, _, fs) = fs
testDatabase :: Database
testDatabase = [("Casino Royale", ["Daniel Craig", "Eva Green", "Judi Dench"], 2006, ["Garry", "Dave", "Zoe", "Kevin", "Emma"]),
("Cowboys & Aliens", ["Harrison Ford", "Daniel Craig", "Olivia Wilde"], 2011, ["Bill", "Jo", "Garry", "Kevin", "Olga", "Liz"]),
("Catch Me If You Can", ["Leonardo DiCaprio", "Tom Hanks"], 2002, ["Zoe", "Heidi", "Jo", "Emma", "Liz", "Sam", "Olga", "Kevin", "Tim"])]
功能一:
displayAllFilms' :: [Film] -> String -> String
displayAllFilms' [] filmString = filmString
displayAllFilms' ((title,cast,year,fans):films) filmString =
displayAllFilms' films (filmString ++ "\n" ++ title ++ ", " ++ listStuff cast ", " ++ (show year) ++ ", " ++ show (length fans))
功能二:
filmsByFan f = map title $ filter (elem f . fans) testDatabase
上面的代码是 100% 工作的,我能够从给定的数据库中提取信息。
这是我创建的示例 MAIN IO:
main :: IO ()
main = do putStrLn "What function would you like to execute? 1. addFilm / 2.displayAllFilms / 3. filmsByYear / 4. filmsByFan / 5. filmsByActor Period / 6. becomeFan: "
str <- getLine
if str /= "2"
then return ()
else do putStrLn (displayAllFilms' testDatabase str)
if str /= "4"
then return ()
else do putStrLn "Enter an existing fan: "
name <- getLine
putStrLn filmsByFan name <<< **error here**
main
我遇到的问题是当超过 2 个函数应用于 IO if 语句时,当用户输入分配“名称”时,它似乎没有注册,因此 name <- getLine 的其余代码没有编译。
我的问题是:有没有办法让用户根据他们选择的内容来选择要执行的功能并执行适当的功能......?
提前致谢!
编辑:
我希望代码如何执行的示例:
Main
which function would you like to execute...1,2,3,4,5,6?
user enters: 1
displayFilms will execute.
IF USER ENTERS 4
user enters: 4
Enter an existing fan:
user enters: Liz"
execute filmsByFan