[Haskell-beginners] (no subject)
Daniel Seidel
seideld at tcs.inf.tu-dresden.de
Wed May 6 10:57:49 EDT 2009
Ashley Banks wrote:
> 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 [])
that part is kind of confusing. My Suggestion: separate functions for
the "submenus". And a main function calling them:
submenu "2" fanName =
do putStr "What film you want to become a fan of? "
filmName <- getLine
putStr (show (becomeFan filmName fanName testDatabase))
return ()
submenu _ _ =
do putStr "The function is not supported yet."
return ()
main :: IO()
main =
do putStr "Hi there! what is your name: "
fanName <- getLine
putStr "1 = Insert film, 2 = Become a Fan, 3 = The number of fans
of a film, 4 = Film released in a year, 5 = Exit: "
input <- getLine
(if input /= "5"
then
do submenu input fanName
main
else return ())
The example should work, but is not very useful, for just adding
yourself to the fan list of a movie in testDatabase. You might need to
pass around the database. It's not like manipulating a global object as
you might do in imperative languages.
>
> // 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
>
>
> ------------------------------------------------------------------------
> Get the New Internet Explore 8 Optimised for MSN. Download Now
> <http://extras.uk.msn.com/internet-explorer-8/?ocid=T010MSN07A0716U>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
More information about the Beginners
mailing list