[Haskell-cafe] The maximum/minimum asymmetry

Sjoerd Visscher sjoerd at w3future.com
Mon Sep 5 16:42:06 CEST 2011


This way these laws hold for non-empty lists:

maximumBy f xs = last (sortBy f xs)
minimumBy f xs = head (sortBy f xs)

Sjoerd Visscher

On Sep 5, 2011, at 6:44 AM, Mario Blažević wrote:

>    I was recently surprised to discover that the maximum and maximumBy functions always return the *last* maximum, while minimum and minimumBy return the *first* minimum in the list. The following GHCi session demonstrates this:
> 
> $ ghci
> GHCi, version 7.2.1: http://www.haskell.org/ghc/  :? for help
> Loading package ghc-prim ... linking ... done.
> Loading package integer-gmp ... linking ... done.
> Loading package base ... linking ... done.
> Loading package ffi-1.0 ... linking ... done.
> Prelude> :module +Data.List Data.Ord
> Prelude Data.List Data.Ord> let list = [(1, 'B'), (1, 'A')]
> Prelude Data.List Data.Ord> maximumBy (comparing fst) list
> (1,'A')
> Prelude Data.List Data.Ord> minimumBy (comparing fst) list
> (1,'B')
> 
>    I would normally consider this kind of gratuitous asymmetry a bug, but seeing that these functions' implementations have been specified in the Haskell 98 Library Report, I guess they are now a permanent feature of the language. Can anybody explain the reason for this behaviour?
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list