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 7877885
In Process

The Archive Base Latest Questions

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

I am trying to change the Film type to a data type so it

  • 0

I am trying to change the Film type to a data type so it will make it easier to order and save and load to a file, I am having problems matching the functions up with the right arguments, it was a lot easier working with the types.

type Rating = (String, Int)
--type Film = (String, String, Int, [Rating])
data Film = Film String String Int [Rating]
         deriving (Show,Ord,Eq, Read)

testDatabase :: [Film]
testDatabase = []

testFilm = ("Test","Test",2012,[("Test",8),("Test",5)])

saveToFile :: IO ()
saveToFile = do
    putStrLn "Enter the output filename: "
    name <- getLine
    writeFile name (show testDatabase)
    putStrLn "Done"

loadFromFile :: IO ()
loadFromFile = do
    putStrLn "Enter the input filename: "
    name <- getLine
    contents <- readFile name
    let testDatabase = length contents `seq` (read contents :: [Film])
    putStrLn formatDatabase
    putStrLn "Done"


average xs = realToFrac (sum xs) / genericLength xs 

addFilm :: String -> String -> Int -> [Film] -> [Film]
addFilm title director year db = db ++ Film title director year []

filmsByDirector :: String -> [Film]
filmsByDirector name = filter (\(a,_,_,_) -> a == name) testDatabase

filmsByRating :: Float -> [Film]
filmsByRating rating = filter (\(_,_,_,a) -> filmRating a > rating) testDatabase

filmsByYear :: Int -> [Film]
filmsByYear year = filter (\(_,_,a,_) -> a == year) testDatabase

filmRating :: [(String,Int)] -> Float
filmRating ratings = average (map snd ratings)

formatString :: Film -> String
formatString (dir, film, year, rat) = printf "\n\nDirector: %s \nFilm Name: %s \nYear: %s \nRating: %4.2f" (show dir) (show film) (show year) (filmRating rat)

formattedByYear :: Int -> String
formattedByYear year = concatMap formatString $ filmsByYear year

formattedByDirector :: String -> String
formattedByDirector dir = concatMap formatString $ filmsByDirector dir

formatDatabase = concatMap formatString $ testDatabase
  • 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:30:20+00:00Added an answer on June 3, 2026 at 3:30 am

    There isn’t much to necessarily change, instead of

    filmsByDirector name = filter (\(a,_,_,_) -> a == name) testDatabase
    

    where you use the (,,,) constructor for the pattern, you use the Film constructor

    filmsByDirector name = filter (\(Film a _ _ _) -> a == name) testDatabase
    

    and similarly for the other functions.

    However, it’s much more convenient to use named-field (or record) syntax and define

    data Film = Film { director, title :: String, year :: Int, ratings :: [Rating] }
    

    and then the function would be simply

    filmsByDirector name = filter ((name ==) . director) testDatabase
    

    or, if you’re uncomfortable with point-free style

    filmsByDirector name = filter (\f -> director f == name) testDatabase
    

    For the error regarding Film vs [Film] in addFilm, you have to wrap the last film into [],

    addFilm title director year db = db ++ [Film title director year []]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to change a button pic, wait a second, then change back. Not having
Trying to change input type attribute from password to text . $('.form').find('input:password').attr({type:text}); Why this
I'm trying to change my web.config appSetting file's name: With the rule: <appSettings file=ExternalAppSettings.Release.config
Trying to change the node/add form for a specific content type to a multistep
i trying to change document content type. I must use workflow, and the best
Im trying to change the thread priority in boost but im having no luck.
i'm trying to change the toolbars order. before netbeans 7 the order was set
I'm trying change an input mask for textbox when the the check box has
Im trying to change images on click similar to what SO does with the
I´m trying to change a spring jsp example to use freemarker. I changed all

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.