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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:55:50+00:00 2026-06-05T13:55:50+00:00

I want to read a whole file into a string and then use the

  • 0

I want to read a whole file into a string and then use the function lines to get the lines of the string. I’m trying to do it with these lines of code:

main = do
   args <- getArgs
   content <- readFile (args !! 0)
   linesOfFiles <- lines content

But I’m getting the following error by compiling ad it fails:

Couldn't match expected type `IO t0' with actual type `[String]'
In the return type of a call of `lines'
In a stmt of a 'do' block: linesOfFiles <- lines content

I thought by binding the result of readFile to content it will be a String DataType, why isn’t it?

  • 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-05T13:55:52+00:00Added an answer on June 5, 2026 at 1:55 pm

    I thought by binding the result of readFile to content it will be a String DataType, why isn’t it?

    It is a String indeed, that’s not what the compiler complains about. Let’s look at the code:

    main = do
       args <- getArgs
       content <- readFile (args !! 0)
    

    Now content is, as desired, a plain String. And then lines content is a [String]. But you’re using the monadic binding in the next line

       linesOfFiles <- lines content
    

    in an IO () do-block. So the compiler expects an expression of type IO something on the right hand side of the <-, but it finds a [String].

    Since the computation lines content doesn’t involve any IO, you should bind its result with a let binding instead of the monadic binding,

       let linesOfFiles = lines content
    

    is the line you need there.

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

Sidebar

Related Questions

I want to read the all the contents of a file into a string
I want to read the last n lines of a big txt file compressed
I want to read a String (Data) out of a text file and write
alright so I am trying to read a text file. and then split it
I want to read the contents of a text file into a char array
I have a text file. I want read that file. But In that if
I want to read a the content of a PDF file and encrypt the
I want to read a file and save it in variable, but I need
am trying to read a file (double values) with scanner and save the data
I'm doing some file validation and want to load an UploadedFile into an external

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.