Since your do
notation is only invoking the Maybe
layer we might think about the (:)
operation as being independent of the Maybe
's effect—it's just yet another function being passed to the value wrapped in Maybe
. That leads us to generalize the code a bit
foo :: (x -> b -> b) -> x -> Maybe b -> Maybe b
foo f x m = do
b <- m
return (f x b)
This makes it much more clear that we're just dealing with an fmap
fmap fx m = do b <- m
return (fx b)
So perhaps the most standard way of writing your desired function is
(z:) <$> process xs ys