
Could anyone look at this segment of code; it's not compiling wondering if anyone could correct me as to why. Thanks Code: -- Film as datatype type Title = String type Director = String type Year = Int type Fan = String data Film = Film Title Director Year [Fan] -- List of films testDatabase :: [Film] testDatabase = [(Film "Casino Royale" "Martin Campbell" 2006 ["Garry", "Dave", "Zoe"]) ] becomeFan :: Title -> fanName -> [Film] -> [Film] becomeFan _ _ [] = [] becomeFan Title fanName ((Film Title Director Year fan):xs) | filmName == title = (Film Title Director Year fanName:fan) : xs | otherwise = (Film Title Director Year fan) : becomeFan Title fanName xs main :: [Film] -> IO() main db = 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, 5 = Given fan, 6 = Stop : " input = getLine x = read input :: Int if x == 1 then do putStr "Enter film title: " filmTitle <- getLine putStr "Enter director name: " filmDirector <- getLine putStr "Enter release year: " filmYear <- getLine main insertFilm [Title Director Year [Film]] else if x == 2 then do putStr "Enter film title: " filmTitle <- getLine putStr "Enter fan name: " fanName <- getLine main becomeFan [Title fanName] else if x == 3 then do putStr "Enter film title: " filmTitle <- getLine main numberOfFans [Title] else if x == 4 then do putStr "Enter film release year: " filmYear <- getLine main filmsInGivenYear [Year [Film]] else if x == 5 then do putStr "Enter the fan name: " fanName <- getLine main givenUser [fanName [Film]] else if x = 6 then return () -- View this message in context: http://www.nabble.com/haskell---main-function-tp23459841p23459841.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

At Sat, 9 May 2009 04:54:13 -0700 (PDT), applebiz89 wrote:
Could anyone look at this segment of code; it's not compiling wondering if anyone could correct me as to why. Thanks
There is a ton of things wrong with that code. I have attached a version that at least compiles, but there are still a bunch of things that are wrong. -- Film as datatype type Title = String type Director = String type Year = Int type Fan = String data Film = Film Title Director Year [Fan] -- List of films testDatabase :: [Film] testDatabase = [(Film "Casino Royale" "Martin Campbell" 2006 ["Garry","Dave", "Zoe"]) ] becomeFan :: Title -> Fan -> [Film] -> [Film] becomeFan _ _ [] = [] becomeFan title' fanName (film@(Film title director year fans) : films) | title == title' = (Film title director year (fanName:fans)) : films | otherwise = film : becomeFan title' fanName films insertFilm = undefined numberOfFans = undefined filmsInGivenYear= undefined givenUser = undefined mainLoop :: [Film] -> IO() mainLoop db = 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 = Given fan, 6 = Stop : " input <- getLine let x = read input :: Int if x == 1 then do putStr "Enter film title: " filmTitle <- getLine putStr "Enter director name: " filmDirector <- getLine putStr "Enter release year: " filmYear <- fmap read getLine mainLoop $ insertFilm (Film filmTitle filmDirector filmYear []) db else if x == 2 then do putStr "Enter film title: " filmTitle <- getLine putStr "Enter fan name: " fanName <- getLine mainLoop $ becomeFan filmTitle fanName db else if x == 3 then do putStr "Enter film title: " filmTitle <- getLine mainLoop $ numberOfFans filmTitle db else if x == 4 then do putStr "Enter film release year: " filmYear <- getLine mainLoop $ filmsInGivenYear filmYear db else if x == 5 then do putStr "Enter the fan name: " fanName <- getLine mainLoop $ givenUser fanName db else if x == 6 then return () else return () main :: IO () main = mainLoop testDatabase

Hi applebiz89 wrote:
Could anyone look at this segment of code; it's not compiling wondering if anyone could correct me as to why. Thanks
Code:
-- Film as datatype type Title = String type Director = String type Year = Int type Fan = String data Film = Film Title Director Year [Fan]
-- List of films
testDatabase :: [Film] testDatabase = [(Film "Casino Royale" "Martin Campbell" 2006 ["Garry", "Dave", "Zoe"]) ]
becomeFan :: Title -> fanName -> [Film] -> [Film] becomeFan _ _ [] = [] becomeFan Title fanName ((Film Title Director Year fan):xs) | filmName == title = (Film Title Director Year fanName:fan) : xs | otherwise = (Film Title Director Year fan) : becomeFan Title fanName xs
main :: [Film] -> IO() main db = 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, 5 = Given fan, 6 = Stop : " input = getLine x = read input :: Int if x == 1 then do putStr "Enter film title: " filmTitle <- getLine putStr "Enter director name: " filmDirector <- getLine putStr "Enter release year: " filmYear <- getLine main insertFilm [Title Director Year [Film]] else if x == 2 then do putStr "Enter film title: " filmTitle <- getLine putStr "Enter fan name: " fanName <- getLine main becomeFan [Title fanName] else if x == 3 then do putStr "Enter film title: " filmTitle <- getLine main numberOfFans [Title] else if x == 4 then do putStr "Enter film release year: " filmYear <- getLine main filmsInGivenYear [Year [Film]] else if x == 5 then do putStr "Enter the fan name: " fanName <- getLine main givenUser [fanName [Film]] else if x = 6 then return ()
You have an if without an else. You cannot have that. What should the program do when x /= 6. And you write "x = 6" in stead of "x == 6". But generally speaking, you want to include compiler output in "this is not compiling"-messages to haskell-cafe. Regards, Mads Lindstrøm

applebiz89 wrote:
becomeFan :: Title -> fanName -> [Film] -> [Film] becomeFan _ _ [] = [] becomeFan Title fanName ((Film Title Director Year fan):xs) | filmName == title = (Film Title Director Year fanName:fan) : xs | otherwise = (Film Title Director Year fan) : becomeFan Title fanName xs
In the type signature, you have to use types. In other words, use "Fan" instead of "fanName" in the first line. Variables have to begin with a lowercase letter. In other words, use "title", "director", "year" instead of "Title", "Director", "Year". Regarding the IO: I still propose to get your pure functions to work first, then worry about IO. You can test your code by loading it into ghci.
ghci nameofmyfile.hs
If everything works (i.e. no compile errors), you can call your functions at the prompt:
becomeFan "Casine Royale" "apple", testDatabase
Now ghci should answer with something like the following: [(Film "Casino Royale" "Martin Campbell" 2006 ["apple", "Garry", "Dave", "Zoe"])] This way, you can test your stuff while working on it. Tillmann
participants (4)
-
applebiz89
-
Jeremy Shaw
-
Mads Lindstrøm
-
Tillmann Rendel