haskell 中的区分错误

Distinguishing errors in haskell

我还是 Haskell 的新手。区分错误的最佳方法是什么? 目前我正在使用 maybe monad 但它只能 'represent one state of error'.

以下代码片段会将我的问题置于上下文中。

pulOh :: Seq          -- input X O sequence
      -> Int          -- split point real index
      -> Maybe Seq
pulOh xs n          =
  case (\(f,l)->(tlTrn f, hdTrn l)) (splSq xs n) of         -- split and process at index
    (Nothing, _)    -> Nothing                              -- first failed
    (_, Nothing)    -> Nothing                              -- last failed
    (Just f,Just l) -> Just (f ++ l)                        -- both parts passed

我希望结果能够区分 fstsnd 的调用是否失败。短路都失败的情况fst失败

使用Either。它与带有参数化 Nothing 构造函数的 Maybe 基本相同,或者换句话说,Maybe aEither () a 同构。通过将 ()“单元错误”替换为自定义错误标记类型,您可以使不同的失败案例有所不同。

pulOh :: Seq -> Int -> Either String Seq
pulOh xs n = case tlTrn *** hdTrn $ splSq xs n of
    (Nothing, _)    -> Left "first failed"
    (_, Nothing)    -> Left "last failed"
    (Just f,Just l) -> Right $ f ++ l

(我冒昧地用 ***“并行管道”替换了那个 lambda)