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).This predicate does not perform any type testing functions.
This predicate works properly for set operations only, so repeated elements and variable elements should not be used in the lists.
Success: intersection([1,2],[2,3],L). (gives L=[2]). intersection([a,d],[a,b,c],[a]). Fail: intersection([a,b],[a,b],[b]).