[Haskell-beginners] a problem with maps

Dennis Raddle dennis.raddle at gmail.com
Sat Jul 23 03:40:20 CEST 2011


In my application I find an issue coming up frequently. I have a map

Ord k => Map k [a]

That is, a list of things may be stored at each key. I sometimes want to
convert it to

[(k,a)]

where each value is paired with its key, and some keys may repeat in that
form of the list. No particular order is needed.

so I have the following code. Would appreciate hearing if there are any
tricks I'm missing

import qualified Map as M

listOutMap :: Ord k => Map k [a] -> [(k,a)]
listOutMap m = concatMap (\(k,a) -> zip (repeat k) a) (M.toList m)

mapOutList :: Ord k => [(k,a)] -> Mpa k [a]
mapOutList list = M.fromList $ map (second (: [])) list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110722/34d41627/attachment.htm>


More information about the Beginners mailing list