Go to the previous, next section.

Ordered Set Operations

This package defines operations on ordered sets. Ordered sets are sets represented as lists with the elements ordered in a standard order. The ordering is defined by the @< family of term comparison predicates and it is the ordering produced by the built-in predicate sort/2 (see section Comparison of Terms).

To load the package, enter the query

| ?- use_module(library(ordsets)).

@
Set is an ordered set.

@
Set is the ordered representation of the set denoted by the unordered representation List. Example:
| ?- list_to_ord_set([p,r,o,l,o,g], P).

P = [g,l,o,p,r] ? 

yes

@
Set2 is Set1 with Element inserted in it, preserving the order. Example:
| ?- ord_add_element([a,c,d,e,f], b, N).

N = [a,b,c,d,e,f] ? 

yes

@
Set2 is like Set1 but with Element removed.

@
The two ordered sets have no elements in common.

@
The two ordered sets have at least one element in common.

@
Intersect is the ordered set representation of the intersection between Set1 and Set2.

@
Intersect is the intersection between Set1 and Set2, and Diff is the difference between Set2 and Set1.

@
Intersection is the ordered set representation of the intersection of all the sets in Sets. Example:
| ?- ord_intersection([[1,2,3],[2,3,4],[3,4,5]], I).

I = [3] ? 

yes

@
is true when Elt is a member of Set.

@
Is true when the two arguments represent the same set. Since they are assumed to be ordered representations, they must be identical.

@
SetProduct is the Cartesian Product of the two Sets. The product is represented as pairs: Elem1-Elem2 where Elem1 is an element from Set1 and Elem2 is an element from Set2. Example
| ?- ord_setproduct([1,2,3], [4,5,6], P).

P = [1-4,1-5,1-6,2-4,2-5,2-6,3-4,3-5,3-6] ? 

yes

@
Every element of the ordered set Set1 appears in the ordered set Set2.

@
Difference contains all and only the elements of Set1 which are not also in Set2. Example:
| ?- ord_subtract([1,2,3,4], [3,4,5,6], S).

S = [1,2] ? 

yes 

@
Difference is the symmetric difference of Set1 and Set2. Example:
| ?- ord_symdiff([1,2,3,4], [3,4,5,6], D).

D = [1,2,5,6] ? 

yes

@
Union is the union of Set1 and Set2.

@
Union is the union of Set1 and Set2, and New is the difference between Set2 and Set1. This is useful if you are accumulating members of a set and you want to process new elements as they are added to the set.

@
Union is the union of all the sets in Sets. Example:
| ?- ord_union([[1,2,3],[2,3,4],[3,4,5]], U).

U = [1,2,3,4,5] ? 

yes

Go to the previous, next section.