I havent done much IO at all in haskell, only within the function itself.
However I want to get the input from the interface for the function and
havent done this before.

// Say I have this as my data type and list of films
data Film = Film String String Int [String]
-- List of films

testDatabase :: [Film]
testDatabase = [(Film "Casino Royale" "Martin Campbell" 2006 ["Garry",
"Dave", "Zoe"]) ]

// with functions such as:

becomeFan :: String -> String -> [Film] -> [Film]
becomeFan _ _ [] = []
becomeFan filmName fanName ((Film title director year fans):xs)
| filmName == title = (Film title director year fanName:fans) : xs
| otherwise = (Film title director year fans) : becomeFan filmName fanName
xs

filmsInGivenYear :: Int -> [Film] -> [String]
filmsInGivenYear filmYear films = [ title | (Film title director year fans)
<- films, year == filmYear]

// I want to ask the user what function they want to use, I have this so far

main :: IO()
do putStr "Hi there! what is your name: "
fanName = getLine
do putStr "1 = Insert film, 2 = Become a Fan, 3 = The number of fans of a
film, 4 = Film released in a year: "
input = getLine
read input :: Int
(if input == 1 then main x = insertFilm [] else if input == 2 then main x =
becomeFan [] else if input == 3 then main x = numberOfFans [])

// I thought using the if statement would work, but now I cant think how to
gather the needed input for the function they have chosen?

thanks

apple

Recommended Answers

All 3 Replies

It's hard to understand what you're trying to do with all that unindented code.

In my main function, I want to ask the user what they would like to do 'become fan' for example, then with their choice initiate the function and ask for the appropriate input for that function. This is the code below:

main :: IO()
do putStr "Hi there! what is your name: "
fanName = getLine
do putStr "1 = Insert film, 2 = Become a Fan, 3 = The number of fans of a
film, 4 = Film released in a year: "
input = getLine
read input :: Int
(if input == 1 then main x = insertFilm [] else if input == 2 then main x =
becomeFan [] else if input == 3 then main x = numberOfFans [])

Say they choose film in a given year function function:

filmsInGivenYear :: Int -> [Film] -> [String]
filmsInGivenYear filmYear films = [ title | (Film title director year fans) <- films, year == filmYear]

I need to ask the user what filmYear they want to insert. But i need to do this from the main function...I chose to do an if statement to choose what function they want, but i dont know where to go from there?


so once it is determined they want to use this function i want to ask them 'what film year: ' and use that for the 'Int' in the filmInGivenYear function

You could use code tags in your messages.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.