Inconsistency between MinimumBy and MaximumBy

When it Data.List.minimumBygets equal in the list items, it selects the one that came first, but maximumByselects the last:

> import Data.List
> import Data.Ord
> minimumBy (const (const EQ)) "Hello world!"
'H'
> maximumBy (const (const EQ)) "Hello world!"
'!'

Is it design or coincidence? Is there a good reason for this behavior?

Note that using this assumption can make the code much more concise - for example, minimumOn length textsinstead of using an explicit timer switch such asmap snd (minimumOn (\(p, t) -> (length t, p)) (zip [0..] texts))

+4
source share
2 answers

The Haskell report has a comment about min and max:

-- note that (min x y, max x y) = (x,y) or (y,x)  
    max x y  
         | x <= y    =  y  
         | otherwise =  x  
    min x y  
         | x <= y    =  x  
         | otherwise =  y

minimumByand maximumByprobably using these, or at least trying to stay consistent with them.

, , min max, , , , ( ). .

- , , , . - , , .

+14

( ), , -

minimum l ≑ head (sort l)
maximum l ≑ last (sort l)

minimumBy c l ≑ head (sortBy c l)
maximumBy c l ≑ last (sortBy c l)

... c , const (const EQ). sort , , , minimum head maximum the last.

+3

Source: https://habr.com/ru/post/1682223/


All Articles