Containers - Data.Map
Monoid instance
Section titled “Monoid instance”Map k v provides a Monoid instance with the following semantics:
memptyis the emptyMap, i.e. the same asMap.emptym1 <> m2is the left-biased union ofm1andm2, i.e. if any key is present both inm1andm2, then the value fromm1is picked form1 <> m2. This operation is also available outside theMonoidinstance asMap.union.
Constructing
Section titled “Constructing”We can create a Map from a list of tuples like this:
A Map can also be constructed with a single value:
There is also the empty function.
Data.Map also supports typical set operations such as union, difference and intersection.
Checking If Empty
Section titled “Checking If Empty”We use the null function to check if a given Map is empty:
Finding Values
Section titled “Finding Values”There are many querying operations on maps.
member :: Ord k => k -> Map k a -> Bool yields True if the key of type k is in Map k a:
notMember is similar:
You can also use findWithDefault :: Ord k => a -> k -> Map k a -> a to yield a default value if the key isn’t present:
Inserting Elements
Section titled “Inserting Elements”Inserting elements is simple:
Deleting Elements
Section titled “Deleting Elements”Importing the Module
Section titled “Importing the Module”The Data.Map module in the containers package provides a Map structure that has both strict and lazy implementations.
When using Data.Map, one usually imports it qualified to avoid clashes with functions already defined in Prelude:
Map.empty -- give me an empty Map