[Haskell-beginners] Printing the entries of a Data.Map m

Daniel Fischer daniel.is.fischer at googlemail.com
Sun May 15 12:52:13 CEST 2011


On Sunday 15 May 2011 09:32:32, Manfred Lotz wrote:
> Hi all,
> I have a map with some two thousand entries. Now I want to print that
> map.
> 
> 
> I could do:
>    print m
> 
> 
> but I like to use my own print function:
>    mapM_ myprint (Map.toList m)
> 
> 
> I may be wrong but having to use Map.toList looks pretty inefficient.

It's pretty efficient. I'm not sure that the compiler can completely fuse 
away the intermediate list, but such allocations are cheap. 
The list is consumed as it's generated, the consumed cells are collected in 
the next gc - the space and time overhead should be negligible.

> 
> Question: I'd like to know if there is a more efficient way to do it?



More information about the Beginners mailing list