[Haskell-beginners] Getting Stuff out of IO Handles

Kyle Murphy orclev at gmail.com
Fri Jun 24 01:05:37 CEST 2011


The short answer is no. The longer answer depends on what you mean by "get
out". You can take the result of an IO action and pass it to a pure function
like so:

do
    result <- someIOAction
    return $ somePureFunction result

or alternately:

   someIOAction >>= return . somePureFunction

If you want to know if you can do something like:

someFunction :: Int -> Int
someFunction n = escapeIO $ someIOAction n

then the answer is you shouldn't. It is technically possible, by using a
function that every experienced haskell developer will tell you to never
ever ever ever ever ever ever ever (get the picture) use, which is
unsafePerformIO, but if you use unsafePerformIO it's likely that your code
won't do what you actually expect it to do, even if the type signatures all
check correctly. The IO Monad serves a very important purpose and implies
certain things about a computation, likewise the absence of the IO Monad
implies certain guarantees, and when you break those guarantees bad things
happen.

-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.


On Thu, Jun 23, 2011 at 11:23, Raphael Päbst
<raphael.paebst at googlemail.com>wrote:

> Hey everyone!
> This is probably a stupid question, but is there a way to get stuff
> out of a IO handle?
> If I do something with an IO handle, reading in data from a file for
> example and then do something with the data, is there a way to get the
> results out of the handle, comparable to the return in a do block?
> Or do all operations on the data have to happen inside the handle?
> Thanks
>
> Raf
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110623/66be794d/attachment.htm>


More information about the Beginners mailing list