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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:52:11+00:00 2026-06-02T01:52:11+00:00

Right now I have two types: type Rating = (String, Int) type Film =

  • 0

Right now I have two types:

type Rating = (String, Int)

type Film = (String, String, Int, [Rating])

I have a file that has this data in it:

"Blade Runner"
"Ridley Scott"
1982
("Amy",5), ("Bill",8), ("Ian",7), ("Kevin",9), ("Emma",4), ("Sam",7), ("Megan",4)

"The Fly"
"David Cronenberg"
1986
("Megan",4), ("Fred",7), ("Chris",5), ("Ian",0), ("Amy",6)

How can I look through then file storing all of the entries into something like FilmDatabase = [Film] ?

  • 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-02T01:52:12+00:00Added an answer on June 2, 2026 at 1:52 am

    Haskell provides a unique way of sketching out your approach. Begin with what you know

    module Main where
    
    type Rating = (String, Int)
    type Film = (String, String, Int, [Rating])
    
    main :: IO ()
    main = do
      films <- readFilms "ratings.dat"
      print films
    

    Attempting to load this program into ghci will produce

    films.hs:8:12: Not in scope: `readFilms'

    It needs to know what readFilms is, so add just enough code to keep moving.

    readFilms = undefined
    

    It is a function that should do something related to Film data. Reload this code (with the :reload command or :r for short) to get

    films.hs:9:3:
        Ambiguous type variable `a0' in the constraint:
          (Show a0) arising from the use of `print'
        ...

    The type of print is

    Prelude> :t print
    print :: Show a => a -> IO ()

    In other words, print takes a single argument that, informally, knows how to show itself (that is, convert its contents to a string) and creates an I/O action that when executed outputs that string. It’s more-or-less how you expect print to work:

    Prelude> print 3
    3
    Prelude> print "hi"
    "hi"

    We know that we want to print the Film data from the file, but, although good, ghc can’t read our minds. But after adding a type hint

    readFilms :: FilePath -> Film
    readFilms = undefined
    

    we get a new error.

    films.hs:8:12:
        Couldn't match expected type `IO t0'
                    with actual type `(String, String, Int, [Rating])'
        Expected type: IO t0
          Actual type: Film
        In the return type of a call of `readFilms'
        In a stmt of a 'do' expression: films <- readFilms "ratings.dat"

    The error tells you that the compiler is confused about your story. You said readFilms should give it back a Film, but the way you called it in main, the computer should have to first perform some I/O and then give back Film data.

    In Haskell, this is the difference between a pure string, say "JamieB", and a side effect, say reading your input from the keyboard after prompting you to input your Stack Overflow username.

    So now we know we can sketch readFilms as

    readFilms :: FilePath -> IO Film
    readFilms = undefined
    

    and the code compiles! (But we can’t yet run it.)

    To dig down another layer, pretend that the name of a single movie is the only data in ratings.dat and put placeholders everywhere else to keep the typechecker happy.

    readFilms :: FilePath -> IO Film
    readFilms path = do
      alldata <- readFile path
      return (alldata, "", 0, [])
    

    This version compiles, and you can even run it by entering main at the ghci prompt.

    In dave4420’s answer are great hints about other functions to use. Think of the method above as putting together a jigsaw puzzle where the individual pieces are functions. For your program to be correct, all the types must fit together. You can make progress toward your final working program by taking little babysteps as above, and the typechecker will let you know if you have a mistake in your sketch.

    Things to figure out:

    • How do you convert the whole blob of input to individual lines?
    • How do you figure out whether the line your program is examining is a title, a director, and so on?
    • How do you convert the year in your file (a String) to an Int to cooperate with your definition of Film?
    • How do you skip blank or empty lines?
    • How do you make readFilms accumulate and return a list of Film data?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I have double numba = 5212.6312 String.Format({0:C}, Convert.ToInt32(numba) ) This will give
Right now I have something like this in NHibernate: Expression.Like(property, value, MatchMode.Anywhere) and that
I have an ObservableCollection<Object> that contains two different types. I want to bind this
I actually have right now two questions: 1) What font faces are preferred for
Right now I have this SQL query which is valid but always times out:
Right now i have a line of code, in vb, that calls a text
Right now I have a mysql database with a table userphone(id, phone, type,userid) and
I have a function that will create a select where clause, but right now
I have json with field that contains two different types. fields:[{value:ZIELONE OKO},{value:{@nil:true}}] I have
Right now I have two activities. My goal is to allow the user to

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.