In general, what you can do is this:
case getValue of
Right x -> Right $ negate x
e -> e
What this does should be clear: it's just like pattern matching in a function argument, but against a value. To do what you need, you have a default case which catches anything not matched, and then return that.
In your particular case, however, you can do something slightly nicer:
negate `fmap` getValue
Or, with import Control.Applicative
, you can use <$>
as a synonym for fmap
(negate <$> getValue
). The fmap
function has type fmap :: Functor f => (a -> b) -> f a -> f b
. For any functor1, fmap
converts a function on ordinary values to a function within the functor. For instance, lists are a functor, and for lists, fmap = map
. Here, Either e
represents a functor which is either an exception Left e
or a value Right a
; applying a function to a Left
does nothing, but applying a function to a Right
applies it within the Right
. In other words,
instance Functor (Either e) where
fmap _ le@(Left _) = le
fmap f (Right r) = Right $ f r
Thus the case
version is the direct answer to your question, but your particular example is more nicely approximated by fmap
.
1: To a first approximation, functors are "containers". If you're not comfortable with the various type classes, I recommed the Typeclassopedia for a comprehensive reference; there are many more tutorials out there, and the best way to get a feel for them is to just play with them. However, the fmap
for specific types is often readily usable (especially, in my opinion, when written <$>
).