Proposal: Max and Min for Monoid (ticket # 1952)
Ross Paterson
ross at soi.city.ac.uk
Sun Dec 2 18:25:54 EST 2007
On Sun, Dec 02, 2007 at 09:57:50AM -0800, Conal Elliott wrote:
> My proposed addition:
>
> -- | Ordered monoid under 'max'.
> newtype Max a = Max { getMax :: a }
> deriving (Eq, Ord, Read, Show, Bounded)
>
> instance (Ord a, Bounded a) => Monoid (Max a) where
> mempty = Max minBound
> Max a `mappend` Max b = Max (a `max` b)
Funny, I was thinking of proposing a Max type that adjoined a synthetic
identity, as we did in the finger tree paper:
data Max a = NoMax | Max a
deriving (Eq, Ord, Read, Show)
instance Ord a => Monoid (Max a) where
mempty = NoMax
NoMax `mappend` b = b
a `mappend` NoMax = a
Max x `mappend` Max y = Max (x `max` y)
and similarly for Min. One could even define
getMax :: Bounded a => Max a -> a
getMax NoMax = minBound
getMax (Max x) = x
More information about the Libraries
mailing list