I have the following construct in my code:
f :: Maybe A -> X
f a = case a of
Nothing -> x
(Just b) -> case b of
Nothing -> y
(Just c) -> case c of
Nothing -> z
(Just d) -> d
I'm not seeing an obvious way to simplify this instead of using nested maybe
functions, which wouldn't make the whole thing look much better. Are there any clever - but still understandable - tricks that would help make this construct more "elegant"?
UPDATED 2
Monad Either
is for youUPDATED 3
If we want to have not
Either
type, we could rewrite function:Despite your constraint about not using
maybe
, I think this looks quite nice:or even better, as @pat suggests in his comment:
Why did the code construct a
Maybe (Maybe (Maybe X))
value in the first place? Unpacking such a value isn't nice, but the real question is, why there even is such a value. Maybe the code would better avoid all those nested Maybes.If you really need to have such a value and need to do different things in all the
Just
/Nothing
cases you'll have to write them all down. But instead of several nestedcase
statements you could combine them into one big pattern match: