library(sets) package represents sets as lists with no
repeated elements. Some of the predicates provided by
this package may return sensible answers if given
arguments that contain repeated elements, but that is a lucky
accident. When in doubt, use
list_to_set/2 to convert from a
list (with possibly repeated elements) to a set. For a list
of predicates related to set manipulation that are not in the
package, see lib-lis-set-pre. For some applications, ordered sets are
more appropriate; see lib-lis-ordsets for more information.
The predicates defined in
library(sets) are described below:
add_element/3may only be used to calculate Set2 given Elem and Set1. However, it is permissible for Set1 to be a list with a variable at the end; in this case,
add_element/3will add new elements to the end of Set2.
del_element/3may only be used to calculate Set2 given Elem and Set1. If Set1 does not contain Elem, Set1 and Set2 will be equal. If Set1 contains more than one copy of Elem (in which case Set1 is not really a set representation), only the first copy of Elem will be removed. See
library(lists)(lib-lis-lists) for a predicate that removes all copies of a given element. When Set1 and Set2 are identical, there are infinitely many Elems that would make this predicate true, so we could not solve for Elem. Therefore, we do not attempt to solve for Elem in any case, which is why it is a
is_set/1does not check for any particular order. If Set is not a proper list,
disjoint/2is the opposite of
select/3works on lists as well as on sets.
select/3 is closely related to the predicate
select/3 is normally used to solve for
Element and Residue,
you can read
) as dq"replace X by Y in S giving R",
) can be read as "replace X by nothing
in S giving R".
| ?- select(a, [a,r,a], R). R = [r,a] ; R = [a,r] ; no | ?- select(a, [a,r,a], e, R). R = [e,r,a] ; R = [a,r,e] ; no
selectchk(+Element, +Set, ?Residue)
library(basics). That is, it locates the first occurrence of Element in Set and deletes it, returning the resulting list in Residue. It is steadfast in Residue.
pairfrom/4is to select pairs of elements from a set without selecting the same pair twice in different orders. To ensure termination, either Set or Residue should be proper.
pairfrom/4works on lists as well as on sets.
subset/2can only be used to test two given sets; it cannot be used to generate subsets.
To generate subsets, use
library(lists) (lib-lis-lists); they will generate
each subset (or each proper subset) (and, for the three-argument
versions, its complement) precisely once, but cannot be used
for testing whether a given set is a subset of another. Note
that they generate sub-sequences; to really generate
sub-sets they would have to enumerate all the permutations of
each subsequence, which would be quite costly.
seteq/2works even if Set1 and Set2 do contain duplicates.
list_to_ord_set/2is faster at converting a list to a set, but the method used by
list_to_set/2preserves as much of the original ordering as possible.
intersection/3, but here it is the elements of Set1 that are in Set2 that are deleted.
| ?- setproduct([b,a], [1,2], Product). Product = [[b-1],[b-2],[a-1],[a-2]]
subtract(Set1, Set2, Diff) append(Diff, Set2, Union.
ordsets.plparallel. This predicate is true when the following are true:
union(Set1, Set2, Union), subtract(Set1, Set2, Difference).
) = 2^length(Set
), so this is only useful for a small Set.
| ?- power_set([a,b], X). X = [[a,b],[a],[b],]