in f#, a map is a special kind of set that associates the values with key. a map is created in a similar way as sets are created.
creating maps
maps are created by creating an empty map using map.empty and adding items using the add function. the following example demonstrates this −
example
(* create an empty map *)
let students =
map.empty. (* creating an empty map *)
add("zara ali", "1501").
add("rishita gupta", "1502").
add("robin sahoo", "1503").
add("gillian megan", "1504");;
printfn "map - students: %a" students
(* convert a list to map *)
let capitals =
[ "argentina", "buenos aires";
"france ", "paris";
"chili", "santiago";
"malaysia", " kuala lumpur";
"switzerland", "bern" ]
|> map.oflist;;
printfn "map capitals : %a" capitals
when you compile and execute the program, it yields the following output −
map - students: map
[("gillian megan", "1504"); ("rishita gupta", "1502"); ("robin sahoo", "1503
");
("zara ali", "1501")]
map capitals : map
[("argentina", "buenos aires"); ("chili", "santiago"); ("france ", "paris");
("malaysia", " kuala lumpur"); ("switzerland", "bern")]
you can access individual elements in the map using the key.
example
(* create an empty map *)
let students =
map.empty. (* creating an empty map *)
add("zara ali", "1501").
add("rishita gupta", "1502").
add("robin sahoo", "1503").
add("gillian megan", "1504");;
printfn "map - students: %a" students
(*accessing an element using key *)
printfn "%a" students.["zara ali"]
when you compile and execute the program, it yields the following output −
map - students: map
[("gillian megan", "1504"); ("rishita gupta", "1502"); ("robin sahoo", "1503
");
("zara ali", "1501")]
"1501"
basic operations on maps
add module name
the following table shows the basic operations on maps −
| member | description |
|---|---|
| add | returns a new map with the binding added to the given map. |
| containskey | tests if an element is in the domain of the map. |
| count | the number of bindings in the map. |
| isempty | returns true if there are no bindings in the map. |
| item | lookup an element in the map. raises keynotfoundexception if no binding exists in the map. |
| remove | removes an element from the domain of the map. no exception is raised if the element is not present. |
| tryfind | lookup an element in the map, returning a some value if the element is in the domain of the map and none if not. |
the following example demonstrates the uses of some of the above functionalities −
example
(* create an empty map *)
let students =
map.empty. (* creating an empty map *)
add("zara ali", "1501").
add("rishita gupta", "1502").
add("robin sahoo", "1503").
add("gillian megan", "1504").
add("shraddha dubey", "1505").
add("novonil sarker", "1506").
add("joan paul", "1507");;
printfn "map - students: %a" students
printfn "map - number of students: %d" students.count
(* finding the registration number of a student*)
let found = students.tryfind "rishita gupta"
match found with
| some x -> printfn "found %s." x
| none -> printfn "did not find the specified value."
when you compile and execute the program, it yields the following output −
map - students: map
[("gillian megan", "1504"); ("joan paul", "1507"); ("novonil sarker", "1506"
);
("rishita gupta", "1502"); ("robin sahoo", "1503");
("shraddha dubey", "1505"); ("zara ali", "1501")]
map - number of students: 7
found 1502.