Basically, we must first decide what problem solving is and what are the difficulties of implementation. So, what if we sort the Score first and then just store the first occurrences in the sorted list with respect to Key ? This should work, look at the haskell implementation:
import Data.List import Data.Function type Key = String type Score = Int data Thing = Thing { key :: Key, score :: Score } deriving (Show) myNub = nubBy ((==) `on` key) mySort = sortBy (compare `on` (negate . score)) selectFinest = myNub . mySort
Now we try to run this in ghci :
Prelude> :load Test.hs [1 of 1] Compiling Main ( Test.hs, interpreted ) Ok, modules loaded: Main. *Main> selectFinest [Thing "a" 7, Thing "b" 5, Thing "a" 10] [Thing {key = "a", score = 10},Thing {key = "b", score = 5}]
Checkout hoogle if you are not sure about the features that I used in the solution. It really takes some time to learn how to use on and these functions.
source share