wiki:FunctionalProgramming/Maybe

Maybe or Optional

data Maybe a = Nothing | Just a
     deriving EQ, Ord
instance Semigroup a => Semigroup (Maybe a) where
    Nothing <> b       = b
    a       <> Nothing = a
    Just a  <> Just b  = Just (a <> b)
instance Semigroup a => Monoid (Maybe a) where
    mempty = Nothing
instance  Functor Maybe  where
    fmap _ Nothing       = Nothing
    fmap f (Just a)      = Just (f a)
instance  Monad Maybe  where
    (Just x) >>= k      = k x
    Nothing  >>= _      = Nothing

    (>>) = (*>)
Last modified 3 years ago Last modified on Apr 5, 2018, 4:34:37 AM
Note: See TracWiki for help on using the wiki.