[Haskell-cafe] Generalized null / zero

Alvaro J. Genial genial at alva.ro
Wed Jan 29 02:25:31 UTC 2014

1. Is there a more general version of `null`? (e.g. for a Monad, Functor,
Applicative, Traversable or the like.) The closest I can come up with is,
in decreasing clunkiness:

    zero :: (MonadPlus m, Eq (m a)) => m a -> Bool
    zero = m == mzero

    zero :: (Alternative f, Eq (f a)) => f a -> Bool
    zero = m == empty

    zero :: (Monoid m, Eq m) => m -> Bool
    zero = m == mempty

Though requiring Eq seems ugly and unnecessary, in theory.

2. In that vein, is there an existing function for "a value or a default if
it's zero"? E.g.:

    orElse :: (Monoid m) => m -> m -> m
    a `orElse` b = if zero a then b else a

Thank you,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140128/10eef7b5/attachment.html>

More information about the Haskell-Cafe mailing list