[Haskell-cafe] A Question about IO monad

Adam Bergmark adam at bergmark.nl
Sun Mar 1 13:17:36 UTC 2015

On Sun, Mar 1, 2015 at 12:58 PM, Nikita Karetnikov <nikita at karetnikov.org>

> > You can indeed write "Maybe a -> a" function easy, because you have Maybe
> > constructors exported for you. But please note that this will lead to
> > non-total function, e.g. it will have to return an error in case of call
> > with Nothing, so you should avoid writing and using functions like this.
> A tiny nitpick: you can get a total function if you return a default
> value instead of erroring out.
What default value would you return for the function `Maybe a -> a`?

- Adam

> >> Sometimes IO do something and return something, i wonder if the return
> >> type, for example is IO Int means it will return an int, could i purely
> >> fetch the int?
> The IO in IO Int means that instead of just returning an Int, a
> computation may produce a side-effect (like writing a string to standard
> output).  Since reasoning about code without side-effects is much
> simpler, you want to separate it from impure code.  That's what IO is
> for.  Once you're inside IO, you're dealing with impure code, so you
> want to keep track of things that rely on it.  You can't* and don't want
> to escape.
> * As Konstantine points out, there are legitimate cases for using
>   unsafePerformIO, like writing an FFI binding to a pure function.
>   Haskell type system can't see whether the function in question is pure
>   or not, so it's tagged with IO.  However, if you know that it's pure,
>   you can explicitly state that by using unsafePerformIO.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150301/3f8dd31d/attachment.html>

More information about the Haskell-Cafe mailing list