Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7875991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:05:39+00:00 2026-06-03T03:05:39+00:00

Possible Duplicate: Haskell – Selectively Adding Lists I have a data type: data Film

  • 0

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) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:05:40+00:00Added an answer on June 3, 2026 at 3:05 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: convert string list to int list in haskell I have a string
Possible Duplicate: Haskell Error: Couldn't match expected type Integer' against inferred type Int' How
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: Java's Interface and Haskell's type class: differences and similarities? When I started
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: Replace individual list elements in Haskell? I have managed to make some
Possible Duplicate: How to make a type with restrictions Is it possible in Haskell
Possible Duplicate: Haskell ranges and floats If I generate a list in ghci like
Possible Duplicate: How to tell if a list is infinite? In Haskell, you can
Possible Duplicate: Haskell vs. procedural programming in the real world Few times I heard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.