Tom
Tom

Reputation: 51

How to add an element to a tuple in a list in haskell?

I have the following types and database:-

type Title = String
type Singer = [String]
type Year = Int
type Fan = String
type Fans = [Fan]

type Song = (Title, Signer, Year, Fans)

type Database = [Song]  

songDatabase :: Database
songDatabase = [("Wrapped up", ["Olly Murs"], 2014, ["Garry", "Dave", "Zoe", "Kevin", "Emma"]),
    ("Someone Like you", ["Adele"], 2011, ["Bill", "Jo", "Garry", "Kevin", "Olga", "Liz"]), 
("Drunk in Love", ["Beyonce", "Jay Z"], 2014, ["tom", "Lucy"])]

I want to add a fan to the last tuple in the list. Do i do this through using addToAl or is there other methods i can use?

Do i have to search for the data and delete it and then add the data I want? or is there a way to just add for example, "John" to the fans in the someone like you tuple.

Upvotes: 0

Views: 3963

Answers (1)

bheklilr
bheklilr

Reputation: 54078

You can't add a tuple to an existing list, all values in Haskell are immutable. Instead, you can construct a new list that contains the values you want. The best way to accomplish this is to first write a function that can add a fan to a single film:

addFan :: Fan -> Song -> Song
addFan fan (title, singer, year, fans) = ???

Then you can write a function updates a particular song in the database:

addFanInDB :: Fan -> Title -> Database -> Database
addFanInDB fan songTitle [] = []
addFanInDB fan songTitle (song:db) = ???

Since this looks very much like homework, I'm not going to give you a full solution since that defeats the purpose of the assignment. You'll need to fill in the ??? yourself.

Upvotes: 4

Related Questions