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

  • Home
  • SEARCH
  • 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 70233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:41:55+00:00 2026-05-10T19:41:55+00:00

I’m currently working on project with Haskell, and have found myself some trouble. I’m

  • 0

I’m currently working on project with Haskell, and have found myself some trouble. I’m supposed to read and insert into a list each line in a ‘dictionary.txt’ file, but I can’t seem to do so. I’ve got this code:

main = do     let list = []     loadNums 'dictionary.txt' list  loadNums location list = do     inh <- openFile location ReadMode     mainloop inh list     hClose inh  mainloop inh list = do      ineof <- hIsEOF inh     if ineof         then return ()         else do              inpStr <- hGetLine inh             inpStr:list             mainloop inh list 

It is supposed to get every line (I know it does get every line, since replacing the ‘inpStr:list’ with a ‘putStrLn inpStr’ works correctly, displaying all lines), and insert it into a list but I get the following error:

Couldn't match expected type `IO' against inferred type `[]' 

Probably because the hGetLine isn’t a String, but a IO String, which I have no idea how to handle in order to obtain a proper string I can insert in my list. I have no idea how this could be solved, or what the problem is exactly, but if anyone has any idea of how to properly get every line in a file into a list, I’d appreciate it.

Thanks in advance!

  • 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. 2026-05-10T19:41:56+00:00Added an answer on May 10, 2026 at 7:41 pm

    In the line where the error happens, Haskell is expecting ‘IO a’, but you are giving it a []. Simplifying things a lot, on a do block on the IO monad, every line is either:

    • Something which returns a value of the ‘IO a’ type; the value of the ‘a’ type within it is discarded (so the ‘a’ is often ‘()’)
    • A <- expression, which does the same thing but instead of discarding the value of the ‘a’ type gives it the name to the left of the <-
    • A let, which does nothing more than give a name to a value

    In that do block, the ‘hGetLine inh’ returns an ‘IO String’, and the String within it is extracted and given the name inpStr. The next line, since it’s neither a let or a <-, should have a type ‘IO a’, which it doesn’t (thus causing the compiler error). What you can do instead, since you already have the String, is a let:

    let list' = inpStr:list 

    This creates a new list consisting of the String followed by the original list, and gives it the name of ‘list’ ‘.

    Change the following line to use ‘list’ ‘ instead of ‘list’ (thus passing it the new list). That line calls (recursively) mainloop, which will read one more line, call itself, and so on. After reading the whole file, it will return something with the ‘IO ()’ type. This ‘IO ()’ will be returned to the do block at loadNums. Congratulations, you just created a list with the lines read from the file, in reverse order (since you were appending to the head of the list), and then did nothing to it.

    If you want to do something to it, change the ‘return ()’ to ‘return list’; the return will generate a value of type ‘IO [String]’, with the list within it (return does nothing more than encapsulating the value), which you can extract at loadNums with the <- syntax.

    The rest is left as an exercise to the reader.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.