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]?
I think you have a fundamental misunderstanding about IO in Haskell. Particularly, you say this:
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:
The way that code is marked as pure or impure is using the type system. When you see a function signature like
you know that this function is pure. If you give it a
Stringit will return anIntand moreover it will always return the sameIntif you give it the sameString. On the other hand, a function signature likeis impure, because the return type of
Stringis marked withIO. ObviouslygetLine(which reads a line of user input) will not always return the sameString, 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 goIOyou can never go back.You can think of
IOas a wrapper. When you see a particular type, for example,x :: IO String, you should interpret that to mean “xis an action that, when performed, does some arbitrary I/O and then returns something of typeString” (note that in Haskell,Stringand[Char]are exactly the same thing).So how do you ever get access to the values from an
IOaction? Fortunately, the type of the functionmainisIO ()(it’s an action that does some I/O and returns(), which is the same as returning nothing). So you can always use yourIOfunctions insidemain. When you execute a Haskell program, what you are doing is running themainfunction, 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:
IOtag (basically, you put it in adoblock)doblock – these are the “pure” functions.mainfunction 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).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
reads a file as a
String. So we can use that to get the contents of the file. The functionsplits a
Stringon newlines, so now you have a list ofStrings, each corresponding to one line of the file. The functionDrops the last element from a list (this will get rid of the final
.on each line). The functiontakes a
Stringand turns it into an arbitrary Haskell data type, such asIntorBool. 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
IOtag. 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
To answer
npfedwardscomment about applyinglinesto the output ofreadFile text.txt, you need to realize thatreadFile text.txtgives you anIO String, and it’s only when you bind it to a variable (usingcontents <-) that you get access to the underlyingString, so that you can applylinesto it.Remember: once you go
IO, you never go back.1 I am deliberately ignoring
unsafePerformIObecause, as implied by the name, it is very unsafe! Don’t ever use it unless you really know what you are doing.