Maps in Standard ML
Maps are Standard ML’s built-in associative data type, sometimes called dictionaries in other languages.
To create an empty map, use an association list [ (key1, val1), (key2, val2) ]
.
Set key/value pairs using typical (name, key) :: map
syntax.
Printing a map will show all of its key/value pairs.
Get a value for a key with List.find
.
If the key doesn’t exist, the zero value of the value type is returned.
The List.length
function returns the number of key/value pairs when called on a map.
To remove a key/value pair, use List.filter
.
To remove all key/value pairs from a map, simply set it to an empty list.
You can also declare and initialize a new map in the same line with association list syntax.
To check if two maps are equal, you can use the List.all
function.
Note that maps appear as a printed list of key/value tuples when printed with print
.
Running the program will output:
Now that we can run and build basic Standard ML programs, let’s learn more about the language.