Maximum and Minimum monoids

Gabriel Gonzalez gabriel439 at gmail.com
Thu Dec 27 21:31:05 CET 2012


On 12/27/2012 02:25 PM, Roman Cheplyaka wrote:
> Wouldn't it be better to have a real algebraic type instead of wrapping
> Maybe?
>
> Something like
>
>    data Maximum a = MinusInfinity | Maximum a
>    data Minimum a = PlusInfinity  | Minimum a
>
> Maximum x is more concise than Maximum (Just x), and MinusInfinity is
> more descriptive than Maximum Nothing. getMaximum/getMinimum functions
> can still return Maybes.
>
> Anyway, I'm +1 to having something along these lines.
>
> Roman
>
Interesting.  Then you can recapitulate the original Maybe API using:

getMaximum :: Maximum a -> Maybe a
getMinimum :: Minimum a -> Maybe a

One slight disadvantage is that there might be a tiny overhead for 
converting between Maximum/Minimum and Maybe, but I doubt that would be 
a bottle-neck in any application.

Another disadvantage is then it sets a precedent for redefining the 
First and Last Monoids to similarly more descriptive types.  When I did 
it the Maybe way I was just copying the way First and Last worked.



More information about the Libraries mailing list