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

Matthias Guedemann matthias.guedemann at ovgu.de
Sun May 15 10:15:42 CEST 2011


Hi Manfred,

> I may be wrong but having to use Map.toList looks pretty inefficient.
> 
> Question: I'd like to know if there is a more efficient way to do it?

I do not know if it is really more efficient (it has to consider each
entry, just like converting to list does), but Data.Map is an instance
of Data.Traversable, which has the traverse function, see:

http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.3.1.0/Data-Traversable.html#t:Traversable

e.g.

Prelude> :m Data.Map Data.Traversable 
Prelude Data.Map Data.Traversable> let mymap = insert 1 "bubu" $ insert 2 "baba" $ empty
Prelude Data.Map Data.Traversable> traverse print mymap
"bubu"
"baba"
fromList [(1,()),(2,())]
Prelude Data.Map Data.Traversable> 


Is this what you meant? You probably want to ingnore the result value of
type 

Prelude Data.Map Data.Traversable> :t (traverse print mymap)
(traverse print mymap) :: (Num t, Ord t) => IO (Map t ())


regards
Matthias



More information about the Beginners mailing list