Not a real answer, just some newbie notes.
You can use type aliases for Director
, Title
and all others types.
type Director = String
type Rate = Double
type Rater = String
type Ratings = [(Rater, Rate)]
type Title = String
type Year = Int
data Film = Film Director Director Year Ratings
deriving (Show,Ord,Eq, Read)
That's why your film
example should looks like:
film = Film "Ridley Scott" "Alien" 1979 [("Mark",5),("Zoe",3)]
If you have a function named filmRating
it should be something like :: Film -> Rate
:
filmRating :: Film -> Rate
filmRating (Film _ _ _ ratings) = average (map snd ratings)
How would I go about getting the average director rating across all films in a list of [Film]?
You want something like directorsAverageRate :: Director -> [Film] -> Rate
.
First of all, you need to filter films by Director
value with some :: Film -> Director
function. For this filtered list you simply get average rate with average . (map filmRating)
.
UPD:
I don't understand how to get the overall average after I have the films involved.
Let's take the definition of average
function from the similar question:
average :: (Real a, Fractional b) => [a] -> b
average xs = realToFrac (sum xs) / genericLength xs
Then
> let one_film = Film "Ridley Scott" "Alien" 1979 [("Mark",5),("Zoe",3)]
one_film :: Film
> let another_film = Film "Ridley Scott" "Alien2" 2979 [("Mark",4),("Zoe",2)]
another_film :: Film
> let films = [one_film, another_film]
films :: [Film]
> average . (map filmRating) $ films
3.5
it :: Double