[Haskell-cafe] haskell - main function

Jeremy Shaw jeremy at n-heptane.com
Sat May 9 10:42:57 EDT 2009


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.

-------------- next part --------------
-- 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


More information about the Haskell-Cafe mailing list