[Haskell-cafe] Main function error
Daniel Fischer
daniel.is.fischer at web.de
Tue May 12 18:17:36 EDT 2009
Am Dienstag 12 Mai 2009 18:59:47 schrieb applebiz89:
> I have compiled each function independently and they have compiled the only
> problem is the main function..
>
> I keep getting the error 'films not defined' and I am not sure why
>
> [code]
>
> type Title = String
> type Director = String
> type Year = Int
> type Fan = String
>
> data Film = Film Title Director Year [Fan] deriving Show
>
> -- List of films
>
> testDatabase :: [Film]
> testDatabase = [ (Film "Casino Royale" "Martin Campbell" 2006 ["Garry",
> "Dave", "Zoe"])]
>
> -- Function
>
> filmsInGivenYear :: Year -> [Film] -> [String]
> filmsInGivenYear year' films = [ title | (Film title director year fans) <-
> films, year == year']
>
> doFilmsInGivenYear :: [Film] -> IO ()
> doFilmsInGivenYear films = do putStrLn "which year?"
> text <- getLine
> let year' = read text :: Int
> let answer = filmsInGivenYear year' films
> print answer
>
> main :: IO ()
> main = do
> doFilmsInGivenYear films
> main
There is no top level definition of films in your module, main doesn't take any parameter,
so films is not in scope in main. You probably meant testDatabase.
However, main is not good even if you fix that, because every iteration of main, you work
on the same database, you can't add new films or fans to films.
You should do something like
main :: IO ()
main = loop testDatabase
loop db = do
actionToRun <- selectAction
newDB <- actionToRun db
loop newDB
selectAction = do
putStrLn $ "Select action to run:\n 1-print films from given year\n 2-add fan for some
film\n ..."
ln <- getLine
let n = read ln
act = case n of
1 -> doFilmsInGivenYear
2 -> becomeFan
....
return act
doFilmInGivenYear :: [Film] -> IO [Film]
doFilmInGivenYear films = do
.... (your code)
return films
becomeFan :: [Film] -> IO [Film]
becomeFan films = do
who <- ask for name of fan
whichFilm <- ask for film
let newFilms = makeFan who whichFilm films
return newFilms
then you can alter your database and have the modified database available in the next
iteration of loop.
>
> [/code]
>
> if the other functions are compiling without this error im not sure as to
> why the main function does not compile because of the films...any light on
> this?
The other functions receive films as a parameter.
>
> Thanks
More information about the Haskell-Cafe
mailing list