[Haskell-beginners] A basic misunderstanding of how to program with IO

Edward Z. Yang ezyang at MIT.EDU
Sat May 8 15:24:38 EDT 2010

Excerpts from Ken Overton's message of Sat May 08 12:20:12 -0400 2010:
> Thanks; I guess my 'problem' is that I naturally gravitate to a function that
> returns *the result of the interaction* rather than IO, but that seems
> impossible anywhere IO is used.

That's correct; there's no (normal) way to write a function IO a -> a.

> So looking at the 'real' interact function, I guess the Haskell way is to
> pass in my function that acts on the data that was read.  I guess that makes
> sense as then I can separate all my 'action' code from IO code.  Is that a
> reasonable understanding?

That is also correct.  Notice the types inside your do-block:

    rsp <- getLine

getLine is of type IO String, but rsp is of type String!  As long as we're in
a do-block (which "keeps" us in the IO monad), we can temporarily extract
the real value without IO to pass to a pure function.


More information about the Beginners mailing list