
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 -- View this message in context: http://www.nabble.com/Haskell-IO-problem-tp23404351p23404351.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On Wed, May 6, 2009 at 4:27 PM, applebiz89
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?
Well, here input is a String. Type of read is read :: (Read a) => String -> a i.e. If a is an instance of the read class, read will be able to produce a value of type a given a String. So you have to do something like x = read input :: Int read input will not destructively convert the type of input to int Alsom I think case x of will be better than if x = 1... --- Ashok `ScriptDevil` Gautham

Hi apple, applebiz89 wrote:
// 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] filmsInGivenYear :: Int -> [Film] -> [String]
// I want to ask the user what function they want to use
Once again, it is important to write small blocks of code first. Let's write an IO wrapper around one of the functions, e.g. around filmsInGivenYear: -- Given a films database, this operation asks the user for a year, -- and then prints the names of all the films from that year. doFilmsInGivenYear :: [Film] -> IO () doFilmsInGivenYear films = do -- an action putStrLn "which year?" -- an action which returns a result text <- getLine -- some pure computations let year = read text :: Int let answer = filmsInGivenYear year films -- an action print answer For now, we want main to be just doFilmsInGivenYear using the testDatabase: main :: IO () main = doFilmsInGivenYear films If you compile and run that program, it should ask you for a year, and then print the films of that year. Then it exists, because there are no more actions to do. If you want the program to go on asking you stuff, you can call main again: main :: IO () main = do doFilmsInGivenYear films main If you compile this program and run it, it will run forever and keep asking you about years. (Strg-C should stop it again). *After you have tried that out*, you could try to add code to main to ask the user whether she wants to stop. Tillmann PS. Please don't send questions only to me, I am not your private tutor :) Send to the list instead. PPS. It is really important to compile your program often, and/or test it with ghci. It is much easier to do one small step after another
participants (3)
-
applebiz89
-
Ashok Gautham
-
Tillmann Rendel