【发布时间】:2016-02-18 23:00:27
【问题描述】:
考虑以下代码:
run = runExcept $ do
case Just 1 of
Nothing -> throwE "escape 1"
Just x -> do
case Just 2 of
Nothing -> throwE "escape 2"
Just y -> do
case Just 3 of
Nothing -> throwE "escape 3"
Just z -> return z
假设Just 1、Just 2、Just 3 是返回Maybe Int 的函数调用。
整个函数都在ExceptT 中,因为我想抛出异常。但里面真的只是很多 Maybe 值被操纵。
那么,我是否有可能利用 Maybe monad 的行为来避免出现阶梯状,同时仍然能够抛出异常?
【问题讨论】:
-
您可能会喜欢
errors包,其中包括note :: a -> Maybe b -> Either a b。
标签: haskell monads monad-transformers