fork download
  1. import Data.Map.Strict as Map
  2.  
  3. keyValuesToMap :: (Eq k, Ord k, Ord v) => [(k, v)] -> Map k [v]
  4. keyValuesToMap x = keyValuesToMap_ x empty
  5.  
  6. keyValuesToMap_ :: (Eq k, Ord k, Ord v) => [(k, v)] -> Map k [v] -> Map k [v]
  7. keyValuesToMap_ [] map = map
  8. keyValuesToMap_ (x:xs) map = keyValuesToMap_ xs map_
  9. where
  10. (key, val) = x
  11. currVal = findWithDefault [] key map
  12. map_ = insert key (currVal ++ [val]) map
  13.  
  14. main = do
  15. print $ keyValuesToMap [(1, "one"), (2, "two"), (1, "uno")]
Success #stdin #stdout 0s 5500KB
stdin
Standard input is empty
stdout
fromList [(1,["one","uno"]),(2,["two"])]