
On Thu, 2010-09-23 at 13:14 -0300, Felipe Lessa wrote:
On Thu, Sep 23, 2010 at 12:58 PM, Jake McArthur
wrote: I think this isn't the right link. It should be
http://hackage.haskell.org/trac/ghc/ticket/1952
-- | 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)
Why should we prefer this monoid over
data Max a = Minimum | Max a deriving (Eq, Ord, Read, Show)
instance Ord a => Monoid (Max a) where mempty = Minimum Minimum `mappend` x = x x `mappend` Minimum = x Max a `mappend` Max b = Max (a `max` b)
Or should we have both variants? Or should we have something like
data AddBounds a = Minimum | This a | Maximum deriving (Eq, Ord, Read, Show)
instance Bounded (AddBounds a) where minBound = Minimum maxBound = Maximum
Cheers! =)
-- Felipe.
The original version: - Uses newtype which would be more efficient - For many practical uses the Min/Max have concrete, 'natural' values - It is analogy to Sum and Product from Data.Monoid - It is simpler to use fromMax :: Max a -> a then fromMax :: Max a -> Maybe a - It shows that the bounded types form a monoid rather then wrappes arbitrary type into monoid Reagrds