haskell - Inconsistency between minimumBy and maximumBy -
when data.list.minimumby
comes across list elements equal, chooses 1 came first, maximumby
chooses last one:
> import data.list > import data.ord > minimumby (const (const eq)) "hello world!" 'h' > maximumby (const (const eq)) "hello world!" '!'
is design or coincidence? there reasoning behind behavior?
note taking advantage of such assumption can make code more succinct - i.e minimumon length texts
instead of using explicit tie-breaker such map snd (minimumon (\(p, t) -> (length t, p)) (zip [0..] texts))
in haskell report there comment min , max:
-- note (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
minimumby
, maximumby
using these or @ least trying stay consistent them.
i guess reason might use min , max to, say, write sort involved comparing , swapping pairs (as in operation in comment above). without property lose elements , have others duplicated.
you wouldn't able observe difference, since things compare equal identical. can imagine having elements sort of internal structure isn't considered comparisons not want lost during sort.
Comments
Post a Comment