Data.List.maximumBy uses counter-intuitive ordering

Wiebe-Marten Wijnja w-m at wmcode.nl
Fri Dec 28 22:25:15 UTC 2018


//>/> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20181228/b5f81115/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20181228/b5f81115/attachment.sig>


More information about the Libraries mailing list