[Haskell-beginners] a problem with maps

Dennis Raddle dennis.raddle at gmail.com
Sat Jul 23 03:41:57 CEST 2011


Oops that was supposed to be

mapOutList list = M.fromListWith (++) $ map (second (: [])) list

On Fri, Jul 22, 2011 at 6:40 PM, Dennis Raddle <dennis.raddle at gmail.com>wrote:

> 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/2d1b972b/attachment.htm>


More information about the Beginners mailing list