Proposal: Max and Min for Monoid

Edward Kmett ekmett at gmail.com
Thu Sep 23 13:53:31 EDT 2010


I supply both variants in the monoids package. They are both useful.

+1 from me for adding either or both sets.

The only real issue is the bikeshed problem of naming them.

-Edward Kmett

On Sep 23, 2010, at 12:14 PM, Felipe Lessa <felipe.lessa at gmail.com> wrote:

> On Thu, Sep 23, 2010 at 12:58 PM, Jake McArthur <jake.mcarthur at gmail.com> wrote:
>> [1] http://hackage.haskell.org/trac/ghc/ticket/1951
> 
> 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.
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries


More information about the Libraries mailing list