F# – Sets

  • Post author:
  • Post category:F#
  • Post comments:1 Comment
F# - Sets

F# sets in F# is a data structure that acts as a collection of items without preserving the order in which items are inserted. Sets do not allow duplicate entries to be inserted into the collection.

Creating F# Sets

Sets can be created in the following ways βˆ’

  • By creating an empty set using Set.empty and adding items using the add function.
  • Converting sequences and lists to sets.

The following program demonstrates the techniques βˆ’

(* creating sets *)
let set1 = Set.empty.Add(3).Add(5).Add(7). Add(9)
printfn"The new set: %A" set1

let weekdays = Set.ofList ["mon"; "tues"; "wed"; "thurs"; "fri"]
printfn "The list set: %A" weekdays

let set2 = Set.ofSeq [ 1 .. 2.. 10 ]
printfn "The sequence set: %A" set2

When you compile and execute the program, it yields the following output βˆ’

The new set: set [3; 5; 7; 9]
The list set: set ["fri"; "mon"; "thurs"; "tues"; "wed"]
The sequence set: set [1; 3; 5; 7; 9]

Basic Operations on Sets

The following table shows the basic operations on sets βˆ’

ValueDescription
add : ‘T β†’ Set<‘T> β†’ Set<‘T>Returns a new set with an element added to the set. No exception is raised if the set already contains the given element.
contains : ‘T β†’ Set<‘T> β†’ boolEvaluate toΒ trueΒ if the given element is in the given set.
count : Set<‘T> β†’ intReturns the number of elements in the set.
difference : Set<‘T> β†’ Set<‘T> β†’ Set<‘T>Returns a new set with the elements of the second set removed from the first.
empty : Set<‘T>The empty set for the specified type.
exists : (‘T β†’ bool) β†’ Set<‘T> β†’ boolTests if any element of the collection satisfies the given predicate. If the input function is the predicate and the elements are i0…iN, then this function computes predicate i0 or … or predicate iN.
filter : (‘T β†’ bool) β†’ Set<‘T> β†’ Set<‘T>Returns a new collection containing only the elements of the collection for which the given predicate returns true.
fold : (‘State β†’ ‘T β†’ ‘State) β†’ ‘State β†’ Set<‘T> β†’ ‘StateApplies the given accumulating function to all the elements of the set.
foldBack : (‘T β†’ ‘State β†’ ‘State) β†’ Set<‘T> β†’ ‘State β†’ ‘StateApplies the given accumulating function to all the elements of the set.
forall : (‘T β†’ bool) β†’ Set<‘T> β†’ boolTests if all elements of the collection satisfy the given predicate. If the input function is p and the elements are i0…iN, then this function computes p i0 && … && p iN.
intersect : Set<‘T> β†’ Set<‘T> β†’ Set<‘T>Computes the intersection of the two sets.
intersectMany : seq<Set<‘T>> β†’ Set<‘T>Computes the intersection of a sequence of sets. The sequence must be non-empty.
isEmpty : Set<‘T> β†’ boolReturns true if the set is empty.
isProperSubset : Set<‘T> β†’ Set<‘T> β†’ boolEvaluate toΒ trueΒ if all elements of the first set are in the second, and at least one element of the second is not in the first.
isProperSuperset : Set<‘T> β†’ Set<‘T> β†’ boolEvaluate toΒ trueΒ if all elements of the second set are in the first, and at least one element of the first is not in the second.
isSubset : Set<‘T> β†’ Set<‘T> β†’ boolEvaluate toΒ trueΒ if all elements of the first set are in the second.
is Superset : Set<‘T> β†’ Set<‘T> β†’ boolEvaluate toΒ trueΒ if all elements of the second set are in the first.
iter : (‘T β†’ unit) β†’ Set<‘T> β†’ unitApplies the given function to each element of the set, in order according to the comparison function.
map : (‘T β†’ ‘U) β†’ Set<‘T> β†’ Set<‘U>Returns a new collection containing the results of applying the given function to each element of the input set.
maxElement : Set<‘T> β†’ ‘TReturns the highest element in the set according to the ordering being used for the set.
minElement : Set<‘T> β†’ ‘TReturns the lowest element in the set according to the ordering being used for the set.
of Array : ‘T array β†’ Set<‘T>Creates a set that contains the same elements as the given array.
ofList : ‘T list β†’ Set<‘T>Creates a set that contains the same elements as the given list.
of Seq : seq<‘T> β†’ Set<‘T>Creates a new collection from the given enumerable object.
partition : (‘T β†’ bool) β†’ Set<‘T> β†’ Set<‘T> * Set<‘T>Splits the set into two sets containing the elements for which the given predicate returns true and false respectively.
remove : ‘T β†’ Set<‘T> β†’ Set<‘T>Returns a new set with the given element removed. No exception is raised if the set doesn’t contain the given element.
singleton : ‘T β†’ Set<‘T>The set containing the given element.
toArray : Set<‘T> β†’ ‘T arrayCreates an array that contains the elements of the set in order.
toList : Set<‘T> β†’ ‘T listCreates a list that contains the elements of the set in order.
toSeq : Set<‘T> β†’ seq<‘T>Returns an ordered view of the collection as an enumerable object.
union : Set<‘T> β†’ Set<‘T> β†’ Set<‘T>Computes the union of the two sets.
union many : seq<Set<‘T>> β†’ Set<‘T>Computes the union of a sequence of sets.

The following example demonstrates the uses of some of the above functionalities βˆ’

Example

let a = Set.ofSeq [ 1 ..2.. 20 ]
let b = Set.ofSeq [ 1 ..3 .. 20 ]
let c = Set.intersect a b
let d = Set.union a b
let e = Set.difference a b

printfn "Set a: "
Set.iter (fun x -> printf "%O " x) a
printfn""

printfn "Set b: "
Set.iter (fun x -> printf "%O " x) b
printfn""

printfn "Set c = set intersect of a and b : "
Set.iter (fun x -> printf "%O " x) c
printfn""

printfn "Set d = set union of a and b : "
Set.iter (fun x -> printf "%O " x) d
printfn""

printfn "Set e = set difference of a and b : "
Set.iter (fun x -> printf "%O " x) e
printfn""

When you compile and execute the program, it yields the following output βˆ’

Set a:
1 3 5 7 9 11 13 15 17 19
Set b:
1 4 7 10 13 16 19
Set c = set intersect of a and b :
1 7 13 19
Set d = set union of a and b :
1 3 4 5 7 9 10 11 13 15 16 17 19
Set e = set difference of a and b :
3 5 9 11 15 17

Next Topic – Click Here

This Post Has One Comment

Leave a Reply