Skip to content

Maps and sets #13

@chris-martin

Description

@chris-martin

As a jumping-off point for an example involving the Map type from containers here is a fairly direct translation from Go By Example:

import qualified Data.Map as Map

main =
  do
    let m = Map.fromList [("k1", 7), ("k2", 13)]
    putStrLn ("map: " ++ show m)
    putStrLn ("v1: " ++ show (Map.lookup "k1" m))
    putStrLn ("size: " ++ show (Map.size m))

    let m' = Map.delete "k2" m
    putStrLn ("map: " ++ show m')
    putStrLn ("v2: " ++ show (Map.lookup "k2" m'))
$ runhaskell maps.hs
map: fromList [("k1",7),("k2",13)]
v1: Just 7
size: 2
map: fromList [("k1",7)]
v2: Nothing

Since Set is closely related to Map, it might be nice to discuss sets at the same time.

We might also want to show using a custom datatype as the key in a map, to demonstrate how you need to derive Eq and Ord.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good for new contributorsPull requests welcome!new exampleThis issue is about writing a new example program.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions