library(sets)
This library module provides operations on sets represented as unordered lists
with no repeated elements.
The ordered representation used in library(ordsets)
is much more
efficient, but these routines were designed before sort/2
entered the language.
Exported predicates:
add_element(
+Element,
+Set1,
-Set2)
del_element(
+Element,
+Set1,
-Set2)
lists:delete/3
. For a
version which fails if Element is not in Set1, use selectchk/3
.
disjoint(
+Set1,
+Set2)
intersect/2
. If either of the arguments
is improper, disjoint/2
will fail.
is_set(
+List)
pairfrom(
?Set,
?Element1,
?Element2,
?Residue)
intersect(
+Set1,
+Set2)
subset(
+Set1,
+Set2)
subseq/3
, subseq0/2
, subseq1/2
in library(lists)
may do what you want (they preserve the order
of elements within a list).
Could be defined as:
subset(Set1, Set2) :- ( foreach(X,Set1), param(Set2) do memberchk(X,Set2) ).
set_order(
+Xs,
+Ys,
-R)
<
, =
, or >
according as Xs is a subset of Ys,
equivalent to Ys, or a superset of Ys.
seteq(
+Set1,
+Set2)
list_to_set(
+List,
-Set)
power_set(
+Set,
-PowerSet)
intersection(
+Set1,
+Set2,
-Intersection)
intersection(Set1, Set2, Intersection) :- ( foreach(X,Set1), fromto(Intersection,S0,S,[]), param(Set2) do (member(X, Set2) -> S0 = [X|S] ; S0 = S) ).
intersection(
+ListOfSets,
-Intersection)
intersection([Set1|Sets], Intersection) :- ( foreach(X,Set1), fromto(Intersection,S0,S,[]), param(Sets) do ( ( foreach(Set,Sets), param(X) do memberchk(X, Set) ) -> S0 = [X|S] ; S0 = S ) ).
subtract(
+Set1,
+Set2,
-Difference)
intersect/3
, but this time it is the elements of Set1 which
are in Set2 that are deleted. Note that duplicated Elements of
Set1 which are not in Set2 are retained in Difference.
Could be defined as:
subtract(Set1, Set2, Difference) :- ( foreach(X,Set1), fromto(Difference,S0,S,[]), param(Set2) do (member(X, Set2) -> S0 = S ; S0 = [X|S]) ).
symdiff(+
Set1,
+Set2,
-Difference)
setproduct(
+Set1,
+Set2,
-CartesianProduct)
setproduct(Set1, Set2, Product) :- ( foreach(H1,Set1), param(Set2), fromto(Product,P1,P3,[]) do ( foreach(H2,Set2), param(H1), fromto(P1,[H1-H2|P2],P2,P3) do true ) ).
disjoint_union(
+Set1,
+Set2,
-Union)
disjoint(Set1, Set2)
and union(Set1, Set2, Union)
,
that is, Set1 and Set2 have no element in command and Union is
their union.
Could be defined as:
disjoint_union(Set1, Set2, Union) :- ( foreach(X,Set1), fromto(Union,[X|S],S,Set2), param(Set2) do nonmember(X, Set2) ).
union(
+Set1,
+Set2,
-Union)
subtract(Set1,Set2,Diff)
and append(Diff,Set2,Union)
,
that is, when Union is the elements of Set1 that do not occur in
Set2, followed by all the elements of Set2.
Could be defined as:
union(Set1, Set2, Union) :- ( foreach(X,Set1), fromto(Union,S0,S,Set2), param(Set2) do (member(X, Set2) -> S0 = S ; S0 = [X|S]) ).
union(
+Set1,
+Set2,
-Union,
-Difference)
union(Set1, Set2, Union)
and subtract(Set1, Set2, Difference)
.
Could be defined as:
union(Set1, Set2, Union, Difference) :- ( foreach(X,Set1), fromto(Union,S0,S,Set2), fromto(Difference,T0,T,[]), param(Set2) do ( member(X, Set2) -> S0 = S, T0 = T ; S0 = [X|S], T0 = [X|T] ) ).
union(
+ListOfSets,
-Union)
union(Sets, Union) :- ( foreach(Set,Sets), param(Answer) do ( foreach(X,Set), param(Answer) do memberchk(X, Answer) ) ), append(Answer, [], Answer), % cauterise it !, Union = Answer.