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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:25:22+00:00 2026-06-06T13:25:22+00:00

I’ ve got a problem with Haskell. I have text file looking like this:

  • 0

I’ ve got a problem with Haskell. I have text file looking like this:

5.
7. 
[(1,2,3),(4,5,6),(7,8,9),(10,11,12)].

I haven’t any idea how can I get the first 2 numbers (2 and 7 above) and the list from the last line. There are dots on the end of each line.

I tried to build a parser, but function called ‘readFile’ return the Monad called IO String. I don’t know how can I get information from that type of string.

I prefer work on a array of chars. Maybe there is a function which can convert from ‘IO String’ to [Char]?

  • 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-06T13:25:23+00:00Added an answer on June 6, 2026 at 1:25 pm

    I think you have a fundamental misunderstanding about IO in Haskell. Particularly, you say this:

    Maybe there is a function which can convert from ‘IO String’ to [Char]?

    No, there isn’t1, and the fact that there is no such function is one of the most important things about Haskell.

    Haskell is a very principled language. It tries to maintain a distinction between “pure” functions (which don’t have any side-effects, and always return the same result when give the same input) and “impure” functions (which have side effects like reading from files, printing to the screen, writing to disk etc). The rules are:

    1. You can use a pure function anywhere (in other pure functions, or in impure functions)
    2. You can only use impure functions inside other impure functions.

    The way that code is marked as pure or impure is using the type system. When you see a function signature like

    digitToInt :: String -> Int
    

    you know that this function is pure. If you give it a String it will return an Int and moreover it will always return the same Int if you give it the same String. On the other hand, a function signature like

    getLine :: IO String
    

    is impure, because the return type of String is marked with IO. Obviously getLine (which reads a line of user input) will not always return the same String, because it depends on what the user types in. You can’t use this function in pure code, because adding even the smallest bit of impurity will pollute the pure code. Once you go IO you can never go back.

    You can think of IO as a wrapper. When you see a particular type, for example, x :: IO String, you should interpret that to mean “x is an action that, when performed, does some arbitrary I/O and then returns something of type String” (note that in Haskell, String and [Char] are exactly the same thing).

    So how do you ever get access to the values from an IO action? Fortunately, the type of the function main is IO () (it’s an action that does some I/O and returns (), which is the same as returning nothing). So you can always use your IO functions inside main. When you execute a Haskell program, what you are doing is running the main function, which causes all the I/O in the program definition to actually be executed – for example, you can read and write from files, ask the user for input, write to stdout etc etc.

    You can think of structuring a Haskell program like this:

    • All code that does I/O gets the IO tag (basically, you put it in a do block)
    • Code that doesn’t need to perform I/O doesn’t need to be in a do block – these are the “pure” functions.
    • Your main function sequences together the I/O actions you’ve defined in an order that makes the program do what you want it to do (interspersed with the pure functions wherever you like).
    • When you run main, you cause all of those I/O actions to be executed.

    So, given all that, how do you write your program? Well, the function

    readFile :: FilePath -> IO String
    

    reads a file as a String. So we can use that to get the contents of the file. The function

    lines:: String -> [String]
    

    splits a String on newlines, so now you have a list of Strings, each corresponding to one line of the file. The function

    init :: [a] -> [a]
    

    Drops the last element from a list (this will get rid of the final . on each line). The function

    read :: (Read a) => String -> a
    

    takes a String and turns it into an arbitrary Haskell data type, such as Int or Bool. Combining these functions sensibly will give you your program.

    Note that the only time you actually need to do any I/O is when you are reading the file. Therefore that is the only part of the program that needs to use the IO tag. The rest of the program can be written “purely”.

    It sounds like what you need is the article The IO Monad For People Who Simply Don’t Care, which should explain a lot of your questions. Don’t be scared by the term “monad” – you don’t need to understand what a monad is to write Haskell programs (notice that this paragraph is the only one in my answer that uses the word “monad”, although admittedly I have used it four times now…)


    Here’s the program that (I think) you want to write

    run :: IO (Int, Int, [(Int,Int,Int)])
    run = do
      contents <- readFile "text.txt"   -- use '<-' here so that 'contents' is a String
      let [a,b,c] = lines contents      -- split on newlines
      let firstLine  = read (init a)    -- 'init' drops the trailing period
      let secondLine = read (init b)    
      let thirdLine  = read (init c)    -- this reads a list of Int-tuples
      return (firstLine, secondLine, thirdLine)
    

    To answer npfedwards comment about applying lines to the output of readFile text.txt, you need to realize that readFile text.txt gives you an IO String, and it’s only when you bind it to a variable (using contents <-) that you get access to the underlying String, so that you can apply lines to it.

    Remember: once you go IO, you never go back.


    1 I am deliberately ignoring unsafePerformIO because, as implied by the name, it is very unsafe! Don’t ever use it unless you really know what you are doing.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a reasonable size flat file database of text documents mostly saved in
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:

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.