haskell,如何解开IO monad [英] haskell, how to unwrap IO monad
问题描述
我可以执行以下操作: runIdentity
, runErrorT
等,以解开内部monad.
但是,如果使用 IO(任何一个字符串整数)
,我该怎么办?如何解开包装?
I can do: runIdentity
, runErrorT
and more to unwrap inner monad.
However, What should I do in case of IO (Either String Int)
? How to unwrap it ?
推荐答案
您不解包 IO a
操作.相反,您将它们包括在 main
操作(其具有 IO
类型,因此可以使用此类操作)中,并且编译器确保 main
为被执行.
You do not unwrap IO a
actions. Instead, you include them in the main
action (which has an IO
type, hence can use such actions) and the compiler ensures that main
is executed.
您可能还会教一些不了解 IO
的函数,该如何处理 IO
;例如,我们有:
You may also teach functions that do not understand IO
how to handle IO
; for example, we have:
fmap :: (a -> b) -> IO a -> IO b
(=<<) :: (a -> IO b) -> IO a -> IO b
因此,如果您有一个使用 Either String Int
的函数,则可以使用上述函数之一来教它如何使用 IO(任何String Int)
.
Thus if you have a function which consumes an Either String Int
, you may use one of the above functions to teach it how to consume an IO (Either String Int)
instead.
为进一步阅读,您可能会喜欢面向根本不在乎的人的IO Monad .(我也喜欢monad教程您可能已经发明了(也许您已经拥有了)和关于monad的全部信息,尽管它们较少直接与此问题相关.)
For further reading, you may enjoy The IO Monad for People Who Simply Don't Care. (I also like the monad tutorials You Could Have Invented Monads! (And Maybe You Already Have.) and All About Monads, though they are less directly relevant to this question.)
这篇关于haskell,如何解开IO monad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!