In addition to lists and sequences, F# provides two related immutable data structures called sets and maps. Unlike lists, sets and maps are unordered data structures, meaning that these collections do not preserve the order of elements as they are inserted, nor do they permit duplicates.

Contents

A set in F# is just a container for items. Sets do not preserve the order in which items are inserted, nor do they allow duplicate entries to be inserted into the collection.

Sets can be created in a variety of ways:

Adding an item to an empty set
The Set module contains a useful function Set.empty which returns an empty set to start with.

Conveniently, all instances of sets have an Add function with the type val Add : 'a -> Set<'a>. In other words, our Add returns a new set containing our new item, which makes it easy to add items together in this fashion:

>Set.empty.Add(1).Add(2).Add(7);;valit:Set<int>=set[1;2;7]

Converting lists and sequences into sets
Additionally, the we can use Set.ofList and Set.ofSeq to convert an entire collection into a set:

Return a new set with an element added to the set. No exception is raised if the set already contains the given element.

val compare : Set<'a> -> Set<'a> -> int

Compare two sets. Places sets into a total order.

val count : Set<'a> -> int

Return the number of elements in the set. Same as "size".

val difference : Set<'a> -> Set<'a> -> Set<'a>

Return a new set with the elements of the second set removed from the first. That is a set containing only those items from the first set that are not also in the second set.

>leta=Set.ofSeq[1..10]letb=Set.ofSeq[5..15];;vala:Set<int>valb:Set<int>>Set.differenceab;;valit:Set<int>=set[1;2;3;4]>a-b;;(* The '-' operator is equivalent to Set.difference *)valit:Set<int>=set[1;2;3;4]

val exists : ('a -> bool) -> Set<'a> -> bool

Test if any element of the collection satisfies the given predicate.

val filter : ('a -> bool) -> Set<'a> -> Set<'a>

Return a new collection containing only the elements of the collection for which the given predicate returns "true".

Return a new collection containing the results of applying the given function to each element of the input set.

val contains: 'a -> Set<'a> -> bool

Evaluates to true if the given element is in the given set.

val remove : 'a -> Set<'a> -> Set<'a>

Return a new set with the given element removed. No exception is raised if the set doesn't contain the given element.

val count: Set<'a> -> int

Return the number of elements in the set.

val isSubset : Set<'a> -> Set<'a> -> bool

Evaluates to "true" if all elements of the first set are in the second.

val isProperSubset : Set<'a> -> Set<'a> -> bool

Evaluates to "true" if all elements of the first set are in the second, and there is at least one element in the second set which is not in the first.

>leta=Set.ofSeq[1..10]letb=Set.ofSeq[5..15]letc=Set.ofSeq[2;4;5;9];;vala:Set<int>valb:Set<int>valc:Set<int>>Set.isSubsetca;;(* All elements of 'c' exist in 'a' *)valit:bool=true>Set.isSubsetcb;;(* Not all of the elements of 'c' exist in 'b' *);;valit:bool=false

openSystemletcapitals=[("Australia","Canberra");("Canada","Ottawa");("China","Beijing");("Denmark","Copenhagen");("Egypt","Cairo");("Finland","Helsinki");("France","Paris");("Germany","Berlin");("India","New Delhi");("Japan","Tokyo");("Mexico","Mexico City");("Russia","Moscow");("Slovenia","Ljubljana");("Spain","Madrid");("Sweden","Stockholm");("Taiwan","Taipei");("Hong Kong","Hong Kong");("USA","Washington D.C.")]|>Map.ofListletrecmain()=Console.Write("Find a capital by country (type 'q' to quit): ")matchConsole.ReadLine()with|"q"->Console.WriteLine("Bye bye")|country->matchcapitals.TryFind(country)with|Some(capital)->Console.WriteLine("The capital of {0} is {1}\n",country,capital)|None->Console.WriteLine("Country not found.\n")main()(* loop again *)main()

Find a capital by country (type 'q' to quit): Egypt
The capital of Egypt is Cairo
Find a capital by country (type 'q' to quit): Slovenia
The capital of Slovenia is Ljubljana
Find a capital by country (type 'q' to quit): Latveria
Country not found.
Find a capital by country (type 'q' to quit): USA
The capital of USA is Washington D.C.
Find a capital by country (type 'q' to quit): q
Bye bye

F# sets and maps are implemented as immutable AVL trees, an efficient data structure which forms a self-balancing binary tree. AVL trees are well-known for their efficiency, in which they can search, insert, and delete elements in the tree in O(log n) time, where n is the number of elements in the tree.