據我所知,您有:
a :: IO (Maybe X)
b :: X -> IO (Maybe Y)
有IO (Maybe a)
和MaybeT IO a
之間的密切關係,即MaybeT
轉換一個到另一個:
MaybeT :: IO (Maybe a) -> MaybeT IO a
和逆操作就是runMaybeT
:
runMaybeT :: MaybeT IO a -> IO (MaybeT a)
在MaybeT單子要執行的組成只是 綁定操作:
MaybeT a >>= (\x -> MaybeT (b x)) :: MaybeT IO Y
這導致MaybeT IO Y
類型的值。要將其轉換回IO (Maybe Y)
只需使用runMaybeT
。
更新
下面是操作員 「撰寫」 一個和b:
andThen :: IO (Maybe a) -> (a -> IO (Maybe b)) -> IO (Maybe b)
andThen a b = runMaybeT $ MaybeT a >>= (\x -> MaybeT (b x))
但是,如果你發現自己使用該運營商很多,也許你 應該重做你的功能,所以你主要在MaybeT IO
monad中工作,然後你可以使用>>=
和一個runMaybeT
在外面。
什麼是'IO Maybe T'?這看起來像一個善良的錯誤。 – augustss
@augustss IO(也許anyRandomType) – 2426021684
那麼,編輯問題後。目前這是很難讀的。 – leftaroundabout