Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse the IO String in Haskell?

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]?

like image 210
Simon Avatar asked Jun 27 '12 15:06

Simon


People also ask

What is the IO type in Haskell?

IO is the way how Haskell differentiates between code that is referentially transparent and code that is not. IO a is the type of an IO action that returns an a . You can think of an IO action as a piece of code with some effect on the real world that waits to get executed.

What is IO action in Haskell?

IO actions are used to affect the world outside of the program. Actions take no arguments but have a result value. Actions are inert until run. Only one IO action in a Haskell program is run ( main ). Do-blocks combine multiple actions together into a single action.

What does the type IO String signify?

putStrLn is a function that takes a single argument of type String and produces a value of type IO (). The IO () signifies an executable action that returns a value of type ().

What does putStr do Haskell?

putStr is much like putStrLn in that it takes a string as a parameter and returns an I/O action that will print that string to the terminal, only putStr doesn't jump into a new line after printing out the string while putStrLn does. putStrLn "Andy!"


1 Answers

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.

like image 200
Chris Taylor Avatar answered Sep 30 '22 11:09

Chris Taylor