Skip to content

Instantly share code, notes, and snippets.

@bmcorser
Created August 25, 2014 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmcorser/8f3b8e0ef1933b8b5b32 to your computer and use it in GitHub Desktop.
Save bmcorser/8f3b8e0ef1933b8b5b32 to your computer and use it in GitHub Desktop.
import qualified Data.Map
import qualified Data.Maybe
listInsert :: Ord k => (k, v) -> Data.Map.Map k [v] -> Data.Map.Map k [v]
listInsert (k,v) acc = if Data.Maybe.isNothing kv then Data.Map.insert k [v] acc else Data.Map.insert k (v:Data.Maybe.fromJust kv) acc
where kv = Data.Map.lookup k acc
fromListToMapList :: Ord k => [(k, a)] -> Data.Map.Map k [a]
fromListToMapList = foldr listInsert Data.Map.empty
{-|
phoneBook =
[("betty","555-2938")
,("betty","342-2492")
,("bonnie","452-2928")
,("patsy","493-2928")
,("patsy","943-2929")
,("patsy","827-9162")
,("lucille","205-2928")
,("wendy","939-8282")
,("penny","853-2492")
,("penny","555-2111")
]
fromListToMapList phoneBook == fromList [("betty",["555-2938","342-2492"])
,("bonnie",["452-2928"])
,("lucille",["205-2928"])
,("patsy",["493-2928","943-2929","827-9162"])
,("penny",["853-2492","555-2111"])
,("wendy",["939-8282"])]
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment