I\'m a Haskell beginner. Let\'s suppose I want to write a function convertKVList that takes a flat list of key-value pairs, where some of the keys might be rep
Hoogle is not the only search engine able to search Haskell libraries by type signatures and it definitely and unfortunately covers only a small part of Hackage. Searching with Hayoo for a type signature [(a,b)]->[(a,[b])] brought up these two implementations:
Concerning your take on the problem, since in your function you already bring up a higher level datastructure (Map), it doesn't make sense to downgrade to a more primitive associative list in the output, because:
Map input, because it's much more effective for dealing with key-value stores, and if you ever find yourself still needing a list you can always utilize the toList in place. Map implies the absence of duplicate keys on type level, which is not any less important, since in Haskell you should always do the maximum proofs using the type-system. This principle is essentially what makes the statement "If it compiles, it works" closest to truth.In other words this is the correct definition of your function:
convertKVList :: (Ord a) => [(a, b)] -> Map a [b]
convertKVList ls =
Map.fromListWith (++) . map (\(x,y) -> (x,[y])) $ ls
Hayooing for that type signature brings a couple of already implemented results too.
Concerning the approaching the problem, it's classic: "Divide and conquer!". Chris has some good points too in his answer.