Reputation: 513
I'm still new to Haskell. Whats the best way to distinguish between errors?
Currently I'm using the maybe
monad but it can only 'represent one state of error'.
The following code snippet will put my question into context.
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
I would like the result to be able to distinguish if the call fails for fst
or snd
. Short-circuiting the case of both failing to fst
fail.
Upvotes: 1
Views: 84
Reputation: 120711
Use Either
. It is basically the same as Maybe
with a parameterised Nothing
constructor, or in other words, Maybe a
is isomorphic to Either () a
. By replacing the ()
“unit error” with a custom error-tag type, you can make different failure cases different.
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
(I took the liberty of replacing that lambda with a ***
“parallel pipeline”)
Upvotes: 4