Data.List.maximumBy uses counter-intuitive ordering

Ryan Reich ryan.reich at gmail.com
Thu Jan 3 20:50:21 UTC 2019


I think this is the right way to go: undefined when the maximum is
duplicated. It is not implausible, for instance, that someone's mental
model or even an alternate implementation of this function would, say, use
the quicksort-like binary search for the maximum, and that really could
give any of the options because quicksort is not stable.

There is no right answer to this question and therefore no answer should be
demanded unless there is a compelling reason of efficiency.

Definitely document this, though.


On Fri, Dec 28, 2018, 07:27 Eric Mertens <emertens at gmail.com wrote:

> Hello,
>
> My opinion on this issue is that code should not be relying on the
> ordering of the choice made by maximumBy or minimumBy.
>
> If we changed something I’d prefer to document that it is undefined what
> element is chosen when two are considered equal by the comparison function.
>
> Code that relies on a particular earlier or later bias should use a
> function that makes it clear in the name that that’s what it’s doing.
> Readers should not be required to memorize the behavior of minimumBy or
> maximumBy in this regard to understand the code they are reading.
>
> Best regards,
> Eric
>
> > On Dec 28, 2018, at 7:18 AM, Johannes Waldmann <
> johannes.waldmann at htwk-leipzig.de> wrote:
> >
> > Dear all,
> >
> > this was brought up on the GHC tracker (not by me)
> >
> > https://ghc.haskell.org/trac/ghc/ticket/15921
> >
> > and it was suggested for discussion here.
> >
> > my summary: Data.List.maximumBy is right-biased,
> > minimumBy is left-biased, and none of this is documented.
> >
> > - J.W.
> >
> > _______________________________________________
> > Libraries mailing list
> > Libraries at haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20190103/491fa9d1/attachment.html>


More information about the Libraries mailing list