We can create a Map from a list of tuples like this:
Map.fromList [("Alex", 31), ("Bob", 22)]
A Map can also be constructed with a single value:
> Map.singleton "Alex" 31
fromList [("Alex",31)]
There is also the empty function.
empty :: Map k a
...
We use the null function to check if a given Map is empty:
> Map.null $ Map.fromList [("Alex", 31), ("Bob", 22)]
False
> Map.null $ Map.empty
True
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:
> Map.member "Alex" $ Map.singleton "Alex" 31
True
> Map.member "Jenny" $ Map.empty
False
notMember is similar:
&g...
Inserting elements is simple:
> let m = Map.singleton "Alex" 31
fromList [("Alex",31)]
> Map.insert "Bob" 99 m
fromList [("Alex",31),("Bob",99)]
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:
import qualified Data.Map as Map
So we'd then prepend Map funct...
Map k v provides a Monoid instance with the following semantics:
mempty is the empty Map, i.e. the same as Map.empty
m1 <> m2 is the left-biased union of m1 and m2, i.e. if any key is present both in m1 and m2, then the value from m1 is picked for m1 <> m2. This operation is also ava...