I'm trying to return a list of the fans who give the same score for all the movies
so my list is for example: [("fan1", "f1", 3), ("fan1", "f2", 3), ("fan2", "f1", 5)]
and this should return ["fan1", "fan2"] because fan1 rated 2 films the same and also fan2 because its the only film it rated
so far I came up with the following code:
f :: [Film] -> [Fan]
f = nub . map (\(f, _, _) -> f) . filter (\(_, _, s) -> s == 5)
but this only gives me the fans who rated a movie 5 stars and I don't know what to do from here. I am trying use Sets and Maps
First, your type
Filmhas a bad name if the fields are a fan-identifier and rating. A better name would beFilmRating.But that isn't anyway a good type to solve you task, because you're thinking in terms of what fan has done XYZ. So the structure should actually rather be something like
or even
To get there, you can first use a combination of
sortByandgroupByto gather all the gradings by the same fan into a list each, likeThis can be simplified further by mapping a function that removes the redundant information of the fan names:
to get
Over this list of lists, you can then use
filterto get the fans that fulfill the property you're interested in. That requires a predicateThis can be done by a similar pattern matching,
With that you can then use