View Problem
Categorise a list
Given the list
Submit a new solution for haskell, fantom, erlang, or cpp
There are 19 other solutions in additional languages (clojure, csharp, fsharp, groovy ...)
[one, two, three, four, five] produce a map {3:[one, two], 4:[four, five], 5:[three]} which sorts elements into map entries based on their length
haskell
import Data.List (groupBy, sortBy)
import Data.Function (on)
groupInPairsBy :: Ord k => (a -> k) -> [a] -> [(k, [a])]
groupInPairsBy f = map (\xs -> (f (head xs), xs)) .
groupBy ((==) `on` f) . sortBy (compare `on` f)
output :: [(Int, [String])]
output = groupInPairsBy length ["one", "two", "three", "four", "five"]
import Data.Function (on)
groupInPairsBy :: Ord k => (a -> k) -> [a] -> [(k, [a])]
groupInPairsBy f = map (\xs -> (f (head xs), xs)) .
groupBy ((==) `on` f) . sortBy (compare `on` f)
output :: [(Int, [String])]
output = groupInPairsBy length ["one", "two", "three", "four", "five"]
Submit a new solution for haskell, fantom, erlang, or cpp
There are 19 other solutions in additional languages (clojure, csharp, fsharp, groovy ...)




