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

intersection(+List1, +List2, ?Common)

Succeeds if Common unifies with the list which contains the common elements of List1 and List2.

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

Description

Common is unified with a list which contains the common elements of List1 and List2.

The definition of this Prolog library predicate is:

intersection([], _, []).
intersection([Head|L1tail], L2, L3) :-
        memberchk(Head, L2),
        !,
        L3 = [Head|L3tail],
        intersection(L1tail, L2, L3tail).
intersection([_|L1tail], L2, L3) :-
        intersection(L1tail, L2, L3).
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 Common does not unify with the list which contains the common elements of List1 and List2.

Resatisfiable

Yes.

Exceptions

Examples


Success:
   intersection([1,2],[2,3],L).     (gives L=[2]).
   intersection([a,d],[a,b,c],[a]).

Fail:
   intersection([a,b],[a,b],[b]).

See Also

subtract / 3, memberchk / 2, union / 3