943,777 Members | Top Members by Rank

Ad:
May 6th, 2009
0

Haskell IO help

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
applebiz89 is offline Offline
7 posts
since Apr 2009
May 7th, 2009
0

Re: Haskell IO help

It's hard to understand what you're trying to do with all that unindented code.
Reputation Points: 231
Solved Threads: 12
Junior Poster
thoughtcoder is offline Offline
139 posts
since Mar 2009
May 7th, 2009
0

Re: Haskell IO help

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
applebiz89 is offline Offline
7 posts
since Apr 2009
May 7th, 2009
0

Re: Haskell IO help

You could use code tags in your messages.
Reputation Points: 231
Solved Threads: 12
Junior Poster
thoughtcoder is offline Offline
139 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Legacy and Other Languages Forum Timeline: Haskell - getting started help
Next Thread in Legacy and Other Languages Forum Timeline: haskell - displaying contents of a list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC