Here is my current code, I want to be able to create a MAIN IO that acts as an interface and allows the user to pick which functions to execute depending on their choice.
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"])]
Function 1:
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))
Function 2:
filmsByFan f = map title $ filter (elem f . fans) testDatabase
The above code is 100% working and I am able to pull the information out of the given database.
Here is the example MAIN IO that I have created:
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
The problem I am having is when more than 2 functions are applied to the IO if statement, it doesn't seem to register when 'name' is assigned by a user input and thus the rest of the code from name <- getLine does not compile.
My question is: Is there a way for the user to select what function to execute depending on what they have chosen and execute the appropriate function...?
Thanks in advance!
Edit:
Example on how I wish the code to execute:
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
"3"will make both "menu items" appear. The solution is of course to use==instead of/=, or acase..ofexpression. One could also use thewhenfunction.main myFilmListArgument), and use it in the interactive functions, rather than the global film list value, so you can pass the updated version back into the loop.