|
|
|
|
|
by JoshTriplett
4807 days ago
|
|
You can also make Maybe an instance of various typeclasses for even nicer syntax: instance Num a => Num (Maybe a) where
(+) = liftM2 (+)
(-) = liftM2 (-)
(*) = liftM2 (*)
abs = liftM abs
signum = liftM signum
negate = liftM negate
fromInteger = Just . fromInteger
> Just 4 + 2 * Just 6
Just 16
> Nothing * 42
Nothing
Notice how the fromInteger method allows you to freely mix Maybe and non-Maybe numbers. |
|