
What I need to do is build a type of database. Firstly have a few questions, whether what I have done it right...and how to do something else. This is my algebraic type for the database (correct me if its wrong) data Film = Film String String Int String with this as the data. testDatabase :: [Film] testDatabase = [("Casino Royale", "Martin Campbell",2006, "Garry, Dave, Zoe")] the last set of strings are the fans to the film. I want to be able to add a new fan, but not sure how to specifically insert it to there this is what I have so far: becomeFan :: String -> String -> [Film] -> [Film] becomeFan = do putStr "Which film are you a fan of: " filmTitle <- getLine do putStr "What is your name: " fanName <- getLine (if filmTitle == filmName then fanName : [Film]) -- View this message in context: http://www.nabble.com/Getting-started---help-tp23314466p23314466.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

First off, is this homework?
You really ought to start with passing your code to GHCi, or the
compiler, to see whether it's accepted before sending questions to the
mailing list. At least if you are interested in getting constructive
replies.
On Thu, Apr 30, 2009 at 12:33 PM, applebiz89
What I need to do is build a type of database. Firstly have a few questions, whether what I have done it right...and how to do something else.
This is my algebraic type for the database (correct me if its wrong)
data Film = Film String String Int String
with this as the data.
testDatabase :: [Film] testDatabase = [("Casino Royale", "Martin Campbell",2006, "Garry, Dave, Zoe")]
the last set of strings are the fans to the film. I want to be able to add a new fan, but not sure how to specifically insert it to there
If the fans is a "set of strings" why not represent them as a list of strings (`[String]`)?
this is what I have so far:
becomeFan :: String -> String -> [Film] -> [Film] becomeFan = do putStr "Which film are you a fan of: " filmTitle <- getLine do putStr "What is your name: " fanName <- getLine (if filmTitle == filmName then fanName : [Film])
The type is wrong, you need to do this in the `IO` monad. A more serious issue is that I don't understand at all what you intend this code to do. It isn't valid Haskell, but I think that instead of battling Haskell you should, at this point, go back to your algorithm. Answer the question, given your type, what steps are needed to add a fan to a single film? Then you can progress to answering, what steps are needed to add a fan to a specific film in a list of films? /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus@therning.org Jabber: magnus@therning.org http://therning.org/magnus identi.ca|twitter: magthe

firstly no it isnt homework. I havent run it through the compiler because I know it won't compile as I havent finished the function as you can see. I was just asking for guidance on how to understand what to do. So now I know to put the fan's into a list, which I have done. So then I can just add the fan's directly to the list of fans if the name of the film matches the desired film to be a fan of. Magnus Therning wrote:
First off, is this homework?
You really ought to start with passing your code to GHCi, or the compiler, to see whether it's accepted before sending questions to the mailing list. At least if you are interested in getting constructive replies.
On Thu, Apr 30, 2009 at 12:33 PM, applebiz89
wrote: What I need to do is build a type of database. Firstly have a few questions, whether what I have done it right...and how to do something else.
This is my algebraic type for the database (correct me if its wrong)
data Film = Film String String Int String
with this as the data.
testDatabase :: [Film] testDatabase = [("Casino Royale", "Martin Campbell",2006, "Garry, Dave, Zoe")]
the last set of strings are the fans to the film. I want to be able to add a new fan, but not sure how to specifically insert it to there
If the fans is a "set of strings" why not represent them as a list of strings (`[String]`)?
this is what I have so far:
becomeFan :: String -> String -> [Film] -> [Film] becomeFan = do putStr "Which film are you a fan of: " filmTitle <- getLine do putStr "What is your name: " fanName <- getLine (if filmTitle == filmName then fanName : [Film])
The type is wrong, you need to do this in the `IO` monad.
A more serious issue is that I don't understand at all what you intend this code to do. It isn't valid Haskell, but I think that instead of battling Haskell you should, at this point, go back to your algorithm. Answer the question, given your type, what steps are needed to add a fan to a single film? Then you can progress to answering, what steps are needed to add a fan to a specific film in a list of films?
/M
-- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus@therning.org Jabber: magnus@therning.org http://therning.org/magnus identi.ca|twitter: magthe _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://www.nabble.com/Getting-started---help-tp23314466p23314963.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Hi, applebiz89 wrote:
data Film = Film String String Int String
with this as the data.
testDatabase :: [Film] testDatabase = [("Casino Royale", "Martin Campbell",2006, "Garry, Dave, Zoe")]
Try to compile this part of the program, to get a feeling for whether you are on the right track. If it does not compile, try to find out what's wrong and correct it so that it compiles. As you already noted in a different mail, you plan to change the representation of the list of fans to a list of strings (instead of a single string with commas). That is a good idea! So change the data declaration, and change the definition of testDatabase to this new design. Try to compile that program. Now you can add some utility functions to work with your database. This will make it much easier to write a correct user interface later. For example, write a function isFan :: [Film] -> String -> String -> Bool which checks whether someone is a fan of a film in your database. In other words, isFan testDatabase "Garry" "Casino Royale" should be True, but isFan testDatabase "Peter" "Matrix" should be False. Another useful utility function could be becomeFan :: String -> String -> [Film] -> [Film] which adds the information that someone is a fan of some film to an existing database. Note that this function does not do any user interaction, but gets the name of the fan and the name of the film as arguments. Think about which other utility functions you could use. Try them out in ghci. When you have done that, you can write a user interface which asks for input on the command line and uses the utility functions to keep track of the current database. Tillmann

Hey thanks, that was really helpful actually. I know it must be annoying for you, and im sorry but ive done what you said, try and compile it to see if it does and fix it. I have tried every possible way of using brackets in different place's etc (as u can tell Im not good with haskell at all) but it still isnt, it must be to do with my data type. -- Film as datatype data Film = String String Int [String] -- List of films testDatabase :: [Film] testDatabase = ["Casino Royale", "Martin Campbell" ,2006, ["Garry", "Dave", "Zoe"] ] I get this error: *** Expression : ["Casino Royale","Martin Campbell",2006,[("Garry","Dave","Zoe")]] *** Term : [("Garry","Dave","Zoe")] *** Type : [([Char],[Char],[Char])] *** Does not match : [Char] Thanks alot! applebiz Tillmann Rendel-3 wrote:
Hi,
applebiz89 wrote:
data Film = Film String String Int String
with this as the data.
testDatabase :: [Film] testDatabase = [("Casino Royale", "Martin Campbell",2006, "Garry, Dave, Zoe")]
Try to compile this part of the program, to get a feeling for whether you are on the right track. If it does not compile, try to find out what's wrong and correct it so that it compiles.
As you already noted in a different mail, you plan to change the representation of the list of fans to a list of strings (instead of a single string with commas). That is a good idea! So change the data declaration, and change the definition of testDatabase to this new design. Try to compile that program.
Now you can add some utility functions to work with your database. This will make it much easier to write a correct user interface later. For example, write a function
isFan :: [Film] -> String -> String -> Bool
which checks whether someone is a fan of a film in your database. In other words,
isFan testDatabase "Garry" "Casino Royale"
should be True, but
isFan testDatabase "Peter" "Matrix"
should be False.
Another useful utility function could be
becomeFan :: String -> String -> [Film] -> [Film]
which adds the information that someone is a fan of some film to an existing database. Note that this function does not do any user interaction, but gets the name of the fan and the name of the film as arguments.
Think about which other utility functions you could use. Try them out in ghci. When you have done that, you can write a user interface which asks for input on the command line and uses the utility functions to keep track of the current database.
Tillmann _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://www.nabble.com/Getting-started---help-tp23314466p23318195.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

I'm intermediate-level myself so I hesitate to say something wrong, but I'll go ahead and point out the obvious. applebiz89 wrote:
Hey thanks, that was really helpful actually. I know it must be annoying for you, and im sorry but ive done what you said, try and compile it to see if it does and fix it. I have tried every possible way of using brackets in different place's etc (as u can tell Im not good with haskell at all) but it still isnt, it must be to do with my data type.
-- Film as datatype
data Film = String String Int [String]
This time you left something off what's called the constructor. (You had it there the first time!) data Film = Film String String Int [String] Because an algebraic data type can be constructed in potentially more than one way, you need a constructor. In this case, you have only one way of constructing a Film, so you just name the constructor Film also.
-- List of films
testDatabase :: [Film] testDatabase = ["Casino Royale", "Martin Campbell" ,2006, ["Garry", "Dave", "Zoe"] ]
And here your problem is that (1) you need to prefix the constructor when making a Film, and (2) don't use commas between arguments to the constructor. (In Haskell commas are for lists and tuples, and not for function arguments.) So this would be how to make one Film: aFilm :: Film aFilm = Film "Casino Royale" "Martin Campbell" 2006 ["Garry", "Dave", "Zoe"] or testDatabase = [ Film "Casino Royale" "Martin Campbell" 2006 ["Garry", "Dave", "Zoe"] ]
I get this error:
*** Expression : ["Casino Royale","Martin Campbell",2006,[("Garry","Dave","Zoe")]] *** Term : [("Garry","Dave","Zoe")] *** Type : [([Char],[Char],[Char])] *** Does not match : [Char]
Thanks alot!
applebiz

On Thu, Apr 30, 2009 at 4:04 PM, applebiz89
Hey thanks, that was really helpful actually. I know it must be annoying for you, and im sorry but ive done what you said, try and compile it to see if it does and fix it. I have tried every possible way of using brackets in different place's etc (as u can tell Im not good with haskell at all) but it still isnt, it must be to do with my data type.
-- Film as datatype
data Film = String String Int [String]
Change that to data Film = Film String String Int [String] that way you get a constructor called `Film` for the data type `Film`: casinoRoyale = Film "CR" "MC" 2006 ["G", "D", "Z"]
-- List of films
testDatabase :: [Film] testDatabase = ["Casino Royale", "Martin Campbell" ,2006, ["Garry", "Dave", "Zoe"] ]
Then you can create your test database either like this: testDatabase = [(Film "CR" "MC" 2006 ["G", "D", "Z"])] or if you have defined `casinoRoyale` like above the like this: testDatabase = [casinoRoyale]
I get this error:
*** Expression : ["Casino Royale","Martin Campbell",2006,[("Garry","Dave","Zoe")]] *** Term : [("Garry","Dave","Zoe")] *** Type : [([Char],[Char],[Char])] *** Does not match : [Char]
I think you would benefit from reading chapter 3 of RWH http://book.realworldhaskell.org/read/defining-types-streamlining-functions.... it offers a good introduction to defining your own types. /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus@therning.org Jabber: magnus@therning.org http://therning.org/magnus identi.ca|twitter: magthe
participants (4)
-
applebiz89
-
Magnus Therning
-
Michael Mossey
-
Tillmann Rendel