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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:49:57+00:00 2026-05-31T13:49:57+00:00

I’m just learning Haskell and I am trying to write some code that simply

  • 0

I’m just learning Haskell and I am trying to write some code that simply reads a file and creates a list of lines using the lines function. For example, I have a file called data.txt that contains the following lines:

this is line one
another line
and the final line

Here is the code I am trying to use to read this data into a list and print it to the screen:

import System.IO  
import Control.Monad

main = do  
        let list = []
        handle <- openFile "data.txt" ReadMode
        contents <- hGetContents handle
        let myLines = lines contents
            list = listLines myLines
        print list
        hClose handle   

listLines :: [String] -> [String]
listLines = map read

The resulting code compiles, but does not produce any output. I get the following output:

runhaskell test.hs        
read_file.hs: Prelude.read: no parse

Can anyone help me to understand what is wrong with my code? Thanks.

  • 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-05-31T13:49:58+00:00Added an answer on May 31, 2026 at 1:49 pm

    As you might have noticed, the error message is telling you there’s problem with read, so let’s focus on that.

    As I said in comments, read takes a string representation of a value of some data type, tries to parse it and return that value. Some examples:

    read "3.14"    :: Double ≡ 3.14    :: Double
    read "'a'"     :: Char   ≡ 'a'     :: Char
    read "[1,2,3]" :: [Int]  ≡ [1,2,3] :: [Int]
    

    Some nonexamples:

    read "[1,2," :: [Int] ≡ error "*** Exception: Prelude.read: no parse"
    read "abc"   :: Int   ≡ error "*** Exception: Prelude.read: no parse"
    

    What happens when you try to use String version of read (i.e. read :: String → String)?

    Haskell’s representation of String (e.g. when you evaluate something that returns String in GHCi) consists of series of characters enclosed in " ... " quotes. Of course, if you want to show some special character (like newline), you have to put the escaped version in there (\n in this case).

    Remember when I wrote that read expects a string representation of a value? In your case, read expects exactly this string format. Naturally, the first thing it tries to do is to match the opening quote. Since your first line doesn’t begin with ", read complains and crashes the program.

    read "hello" :: String fails in the same way that read "1" :: [Int] fails; 1 alone cannot be parsed as list of Ints – read expects the string to start with opening bracket [.


    You might have also heard of show, which is the inverse (but in very loose sense) to read. As a rule of thumb, if you want to read a value x, the string representation read expects looks like show x.


    If you were to change the content of the file to following

    "this is line one"
    "another line"
    "and the final line"
    

    your code would work just fine and produce following input:

    ["this is line one","another line","and the final line"]
    

    If you don’t want to change your .txt file, just remove list = listLines myLines and do print myLines. However, when you run the program, you’ll get

    ["this is line one","another line","and the final line"]
    

    again. So what’s the issue?

    print = putStrLn ∘ show and the default behaviour of show when it comes to showing list of something (i.e. [a] for some a; with exception of Char, which gets special treatment) is to produce string [ firstElement , secondElement ... lastElement ]. As you can see, if you want to avoid the [ ... ], you have to merge [String] back together.

    There’s nifty function called unlines, which is the inverse of lines. Also note, that print calls show first, but we do not want that in this case (we got the string we wanted already)! So we use putStrLn and we’re done. Final version:

    main = do  
        handle <- openFile "data.txt" ReadMode
        contents <- hGetContents handle
        let myLines = lines contents
        putStrLn (unlines myLines)
        hClose handle
    

    We could also get rid of the unneeded lines ~ unlines and just putStrLn contents.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

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.