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]?
If you have a parser of this type:
and you read the file using
then you can read and parse the file using
The result of that will have type
IO Foo
.The
fmap
meansmyParser
runs "inside" the IO.Another way to think of it is that whereas
myParser :: String -> Foo
,fmap myParser :: IO String -> IO Foo
.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
String
it will return anInt
and moreover it will always return the sameInt
if you give it the sameString
. On the other hand, a function signature likeis impure, because the return type of
String
is 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 goIO
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 typeString
" (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 functionmain
isIO ()
(it's an action that does some I/O and returns()
, which is the same as returning nothing). So you can always use yourIO
functions insidemain
. When you execute a Haskell program, what you are doing is running themain
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:
IO
tag (basically, you put it in ado
block)do
block - these are the "pure" functions.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).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
String
on newlines, so now you have a list ofString
s, 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
String
and turns it into an arbitrary Haskell data type, such asInt
orBool
. 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
To answer
npfedwards
comment about applyinglines
to the output ofreadFile text.txt
, you need to realize thatreadFile text.txt
gives 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 applylines
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.As people already say, if you have two functions, one is
readStringFromFile :: FilePath -> IO String
, and another isdoTheRightThingWithString :: String -> Something
, then you don't really need to escape a string fromIO
, since you can combine this two functions in various ways:With
fmap
forIO
(IO
isFunctor
):With
(<$>)
forIO
(IO
isApplicative
and(<$>) == fmap
):With
liftM
forIO
(liftM == fmap
):With
(>>=)
forIO
(IO
isMonad
,fmap == (<$>) == liftM == \f m -> m >>= return . f
):With
do
notation:Every time you will get
IO Something
.Why you would want to do it like that? Well, with this you will have pure and referentially transparent programs (functions) in your language. This means that every function which type is IO-free is pure and referentially transparent, so that for the same arguments it will returns the same values. For example,
doTheRightThingWithString
would return the sameSomething
for the sameString
. HoweverreadStringFromFile
which is not IO-free can return different strings every time (because file can change), so that you can't escape such unpure value fromIO
.As a programming noob, I too was confused by
IO
s. Just remember that if you goIO
you never come out. Chris wrote a great explanation on why. I just thought it might help to give some examples on how to useIO String
in a monad. I'll use getLine which reads user input and returns anIO String
.All this does is bind the user input from
getLine
to a value namedline
. If you type this this in ghci, and type:type line
it will return:But wait!
getLine
returns anIO String
So what happened to the
IO
ness fromgetLine
?<-
is what happened.<-
is yourIO
friend. It allows you to bring out the value that is tainted by theIO
within a monad and use it with your normal functions. Monads are easily identified because they begin withdo
. Like so:If you're like me, you'll soon discover that
liftIO
is your next best monad friend, and that$
help reduce the number of parenthesis you need to write.So how do you get the information from
readFile
? Well ifreadFile
's output isIO String
like so:Then all you need is your friendly
<-
:Now if type that in ghci and check the type of
yourdata
you'll notice it's a simpleString
.