[Haskell-cafe] A Question about IO monad

Konstantine Rybnikov k-bx at k-bx.com
Sun Mar 1 10:25:06 UTC 2015


Hi Zongzhe,

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.

IO case is even less safe, since IO is a special monad (its constructor is
not exported), and you should want to do something like this even less, but
still, you have functions like unsafePerformIO that have type "IO a -> a".
But most probably if you need it -- you're doing something wrong.

If you got to "IO a" value -- you should continue writing functions that
operate in IO, and if you want to apply pure function to inner value, you
can do it like this:

main = do
  i <- ioIntVal
  let newIntVal = myPureIntFunc i
  putStrLn (show i)
 1 бер. 2015 01:44, користувач "Zongzhe Yuan" <psyzy3 at nottingham.ac.uk>
написав:

> Hi
> i have a question about IO monad.
> 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?
> but i fail, i cannot construct a function, i use do notation but i found i
> must use something to pass the int to another monad.
> But i could write a function has the type of Maybe a -> a and it is easily.
> Maybe monad and IO monad both instance monad but why i can’t do that?
>
>
> Zongzhe Yuan
>
>
>
> This message and any attachment are intended solely for the addressee
> and may contain confidential information. If you have received this
> message in error, please send it back to me, and immediately delete it.
>
> Please do not use, copy or disclose the information contained in this
> message or in any attachment.  Any views or opinions expressed by the
> author of this email do not necessarily reflect the views of the
> University of Nottingham.
>
> This message has been checked for viruses but the contents of an
> attachment may still contain software viruses which could damage your
> computer system, you are advised to perform your own checks. Email
> communications with the University of Nottingham may be monitored as
> permitted by UK legislation.
>
> _______________________________________________
> 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/3f485f38/attachment.html>


More information about the Haskell-Cafe mailing list