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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -