Possible Duplicate:
Haskell – Selectively Adding Lists
I have a data type:
data Film = Film String String Int [Rating]
deriving (Show,Ord,Eq, Read)
A sample film:
("Ridley Scott","Alien",1979,[("Mark",5),("Zoe",3)])
How would I go about getting the average director rating across all films in a list of [Film]?
So if Ridley Scott had 2 films which both individually had an average score of 5 then he would score an overall average of 5.
I only have the code to work out a single Film
filmRating :: [(String,Int)] -> Float
filmRating ratings = average (map snd ratings)
Not a real answer, just some newbie notes.
You can use type aliases for
Director,Titleand all others types.That’s why your
filmexample should looks like:film = Film "Ridley Scott" "Alien" 1979 [("Mark",5),("Zoe",3)]If you have a function named
filmRatingit should be something like:: Film -> Rate:You want something like
directorsAverageRate :: Director -> [Film] -> Rate.First of all, you need to filter films by
Directorvalue with some:: Film -> Directorfunction. For this filtered list you simply get average rate withaverage . (map filmRating).UPD:
Let’s take the definition of
averagefunction from the similar question:Then