Data.List.maximumBy uses counter-intuitive ordering

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Data.List.maximumBy uses counter-intuitive ordering

Wiebe-Marten Wijnja

> > my summary: Data.List.maximumBy is right-biased,
> > minimumBy is left-biased, and none of this is documented.

> Btw. Data.Semigroup exports Min and Max that are both left-biased

The difference between these two pairs of functions that are very similar 
(and one will with high probability first or more frequently use `Data.Semigroup.min/max` until one encounters a data-structure that 
e.g. needs to be sorted in different ways in different contexts, which is when minimumBy/maximumBy enter the stage.)

All five Haskell developers I know personally that I asked about what they expected of the code snippet (that was also given in the GHC issue, #15921):

Data.List.maximumBy (Data.Ord.comparing snd) [(0, 1), (3, 2), (2, 2), (1, 1)]

expected it to use right-biased ordering in the implementation. (making the outcome `(3, 2)` whereas in reality it will be `(2, 2)`

I believe that we should:

- align the implementations of minimumBy/maximumBy with Data.Semigroup.min/max to make sure it follows the principle of least surprise. 
- If not possible (for instance, if there is a good reason that is as of yet unknown to me to keep the current behaviour), we should improve the current documentation with the current bias behaviour.
  (or potentially instead make it explicitly implementation-defined with a statement like 'you should not assume a specific ordering between items that are considered equal by the chosen relation').

Thank you,

~Wiebe-Marten Wijnja/Qqwy

Libraries mailing list
[hidden email]

signature.asc (849 bytes) Download Attachment