I have two values, t1
and t2
, of type Either String Type
. The Left
-value is used for error handling. These values are used in a function which returns Either String Type
.
What I want to do is check if both t1
and t2
are Right
-values and satisfy p :: Type -> Bool
. If they do, I want to return Right (the type inside t1)
. If both t1
and t2
are Right
-values, but do not satisfy p
, I want to return Left someString
. If one of t1
or t2
is a Left
value, I just want to pass on that value.
How can I do this in an elegant way? I have a hunch that using Either as a monad is the right thing to do, but I'm not sure of how to go about it.
Why monads?
test p (Right t1) (Right t2) | p t1 && p t2 = Right t1
| otherwise = Left "nope"
test _ (Left t1) _ = Left t1
test _ _ (Left t2) = Left t2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With