I am trying to modify an element in the list in the database to add to ratings, I want to return a modified database with the new element in it. I understand Haskell has immutable things but at the same time I can’t quite grasp how to do it.
Here are the types:
data Film = Film Director Title Year Ratings
deriving (Show,Ord,Eq, Read)
testDatabase :: [Film]
The only code I have is:
--addRating :: Rating -> Film -> Film
--addRating rating (Film name director year ratings)= (Film name director year [(ratings : rating)])
--findFilm name = head $ filter (\(Film n _ _ _) -> n == name) testDatabase
The find film works well but I can’t get the addRating to work, even if it did work I still don’t understand how to mesh it all together to have a function to call to return a list of Film that has the element with the new ratings on it.
You almost had it correct:
At the moment you have the code:
The
:operator has typea -> [a] -> [a], it takes one element and appends it to the start of the list, not the end of the list, so you need to switch your arguments around.Also, the syntax
[x], when used like you have, creates a list consisting of one element. If you use it here it will take the list you have created and wrap it inside another list, which is not what you want.You just need to make a list consisting of the
ratingprepended to the rest of theratings: