[ ECLiPSe Lists Library predicate.|Group Index| Full Index]

subtract(+List1, +List2, ?Remainder)

Succeeds if Remainder is the list which contains those elements of List1 which are not in List2.

+List1
List.
+List2
List.
?Remainder
List or variable.

Description

Unifies Remainder with a list containing those elements of List1 which are not in List2.

The definition of this Prolog library Predicate is:

subtract([], _, []).
subtract([Head|Tail], L2, L3) :-
        memberchk(Head, L2),
        !,
        subtract(Tail, L2, L3).
subtract([Head|Tail1], L2, [Head|Tail3]) :-
        subtract(Tail1, L2, Tail3).
The following should be noted:

1. This predicate does not perform any type testing functions.

2. This predicate is not protected by ECLiPSe and may be redefined without generating a warning message.

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

Fail Conditions

Fails if if Remainder does not unify with the list which contains those elements of List1 which are not in List2.

Resatisfiable

No.

Exceptions

Examples


Success:
   subtract([1,2,3,4],[1],R).     (gives R=[2,3,4]).
   subtract([1,2,3],[3,4],R).     (gives R=[1,2]).
   subtract([1,1,2,3],[2],[1,1,3]).
Fail:
   subtract([1,1,2,3],[1],[1,2,3]). % Fails - List2 and
                                    % Remainder share elements


See Also

intersection / 3, union / 3