Haskell/Libraries/Maps

The module  provides the   datatype, which allows you to store values attached to specific keys. This is called a lookup table, dictionary or associative array in other languages.

Motivation
Very often it would be useful to have some kind of data structure that relates a value or list of values to a specific key. This is often called a dictionary after the real-world example: a real-life dictionary associates a definition (the value) to each word (the key); we say the dictionary is a map from words to definitions. A filesystem driver might keep a map from filenames to file information. A phonebook application might keep a map from contact names to phone numbers. Maps are a very versatile and useful datatype.

Why not just ?
You may have seen in other chapters that a list of pairs (or 'lookup table') is often used as a kind of map, along with the function. So why not just use a lookup table all the time? Here are a few reasons:


 * Working with maps gives you access to a whole load more useful functions for working with lookup tables.
 * Maps are implemented far more efficiently than a lookup table would be, specially in terms of lookup speed.

Library functions
The module Data.Map provides an absolute wealth of functions for dealing with Maps, including setlike operations like unions and intersections. The full list can be found in the core libraries documentation.

Example
The following example implements a password database. The user is assumed to be trusted, so is not authenticated and has access to view or change passwords.