View Category

Define an empty map

haskell
import qualified Data.Map as M

emptyMap = M.empty

Define an unmodifiable empty map

haskell
import qualified Data.Map as Map

output :: Map.Map k v
output = Map.empty

Define an initial map

Define the map {circle:1, triangle:3, square:4}
haskell
import qualified Data.Map as M

initialMap = M.fromList [("circle", 1), ("triangle", 3), ("square", 4)]

Check if a key exists in a map

Given a map pets {joe:cat,mary:turtle,bill:canary} print "ok" if an pet exists for "mary"
haskell
import qualified Data.Map as M
import Control.Monad (when)

pets = M.fromList [("joe", "cat"), ("mary", "turtle"), ("bill", "canary")]

checkMary = when (M.member "mary" pets) (print "ok")

Retrieve a value from a map

Given a map pets {joe:cat,mary:turtle,bill:canary} print the pet for "joe" ("cat")
haskell
import qualified Data.Map as M

pets = M.fromList [("joe", "cat"), ("mary", "turtle"), ("bill", "canary")]
retrieve = print $ M.findWithDefault "Not found" "joe" pets

Add an entry to a map

Given an empty pets map, add the mapping from "rob" to "dog"
haskell
import qualified Data.Map as M

pets = M.insert "rob" "dog" M.empty

Remove an entry from a map

Given a map pets {joe:cat,mary:turtle,bill:canary} remove the mapping for "bill" and print "canary"
haskell
import qualified Data.Map as M

main = do
let pets = M.fromList [("joe", "cat"), ("mary", "turtle"), ("bill", "canary")]
pets2 = M.delete "bill" pets
print $ maybe "" id (M.lookup "bill" pets)
print pets2

Create a histogram map from a list

Given the list [a,b,a,c,b,b], produce a map {a:2, b:3, c:1} which contains the count of each unique item in the list
haskell
import Data.List
import qualified Data.Map as Map

histogram :: Ord a => [a] -> Map.Map a Int
histogram xs = Map.fromList [ (head l, length l) | l <- group (sort xs) ]

output :: Map.Map String Int
output = histogram ["a","b","a","c","b","b"]
import Control.Arrow
import Data.List
import qualified Data.Map as Map
import System.Random

histogram :: Ord a => [a] -> Map.Map a Int
histogram = Map.fromList . map (head &&& length) . group . sort

main = print . histogram . take 1000 . randomRs (1::Int, 100) =<< newStdGen
import qualified Data.Map as Map

histogram :: [String] -> Map.Map String Int
histogram ss = foldl addElem Map.empty ss
where addElem m e = Map.insertWith (+) e 1 m

output :: Map.Map String Int
output = histogram ["a","b","a","c","b","b"]

Categorise a list

Given the list [one, two, three, four, five] produce a map {3:[one, two], 4:[four, five], 5:[three]} which sorts elements into map entries based on their length
haskell
import qualified Data.Map as Map

groupInMapBy :: Ord k => (a -> k) -> [a] -> Map.Map k [a]
groupInMapBy f = foldr (\a -> Map.insertWith (++) (f a) [a]) Map.empty

output :: Map.Map Int [String]
output = groupInMapBy length ["one", "two", "three", "four", "five"]
import Data.List (groupBy, sortBy)
import Data.Function (on)

groupInPairsBy :: Ord k => (a -> k) -> [a] -> [(k, [a])]
groupInPairsBy f = map (\xs -> (f (head xs), xs)) .
groupBy ((==) `on` f) . sortBy (compare `on` f)

output :: [(Int, [String])]
output = groupInPairsBy length ["one", "two", "three", "four", "five"]