Reputation: 49
I have a list reviews and that list is filled with:
data Review = Review artist::String score::Integer Tour String String Locatie [String]
So what I'm trying to do is to calculate the average score of the reviews from a specific artist. What I have so far is a function to filter also the reviews of a artist like so:
filterByArtiest :: String -> [Review] -> [Review]
filterByArtiest art = filter (\a -> artiest a == art)
And this is what I had in mind to calculate the average:
gem :: String -> (String -> [Review] -> [Review]) -> Double
gem art =
But I have like no idea how to finish that function to calculate the average.
Upvotes: 2
Views: 1082
Reputation: 8136
First, you need to filter your list to get just the reviews for a partticular artist. If your list of reviews is called reviews
, then something like this should work:
joesReviews = filter (\review -> artist review == "Joe Bloggs") reviews
Next, extract all of the scores into a list.
scores = map score joesReviews
Now you can use sum scores
to determine the total score. Then use length scores
to calculate the number of values. Now you have everything you need to calculate the average.
Upvotes: 1
Reputation: 48664
What you have to do is simple:
Assuming that your Review record is something like this:
data Review = Review {
artist::String,
score::Integer,
tour :: String ,
locatie :: [String]
}
You can calculate the average using this function:
average :: String -> [Review] -> Double
average artistName reviews = fromIntegral (sum scores) / fromIntegral (length scores)
where artists = filterByArtiest artistName reviews
scores = map score artists
Upvotes: 2