[ library(lists) | Reference Manual | Alphabetic Index ]

subset(?SubList, +List)

Succeeds if List is the list which contains all elements from SubList in the same order as in SubList.
?SubList
A term which unifies with a list.
+List
A term which unifies with a list.

Description

Used to test if a specified list contains all elements of another list, or to generate all sublists of a given list.

The definition of this Prolog library predicate is:

        subset([],[]).
        subset([X|L],[X|S]) :-
            subset(L,S).
        subset(L, [_|S]) :-
            subset(L,S).
This predicate does not perform any type testing functions.

This predicate works properly for set operations only, so repeated elements, variable elements and unsorted lists should not be used.

Modes and Determinism

Fail Conditions

Fails if SubList does not unify with a list whose elements are all contained in List in the same order as in SubList.

Resatisfiable

Yes.

Examples

Success:
      subset([1,3], [1,2,3]).
      subset(X, [1,3,4]).        % backtracks over all subsets

Fail:
      subset([2,1], [1,2,3]).   % different order



See Also

union / 3, subtract / 3, intersection / 3