I'm having trouble trying to debug this code of mine to find the intersection between two lists...
For Example:
List1 = [3, 4, 5, 6]
and
List2 = [5, 1, 0, 2, 4]
.
So, the intersecting lines would be stored into List3
would be [4, 5]
.
So here's the code for Prolog.
Any help would be appreciated!!!
setIntersection([], [], []).
setIntersection([], _, []).
setIntersection([X|Xs], Y, [Z|W]) :-
keepDuplicates(X, Y, [Z|Zs]),
setIntersection(Xs, Y, W).
keepDuplicates(_, [], []).
keepDuplicates([], _, []).
keepDuplicates([], [], []).
% Check if the head of the first list is not a match to the
% first head of the second list
keepDuplicates(G, [H|Hs], Line) :-
G \= H,
keepDuplicates(G, Hs, Line).
% Check if the head of the first list
% Does match to the head of the second list
keepDuplicates(G, [G|Gs], [G|NewLine]) :-
keepDuplicates(G, Gs, NewLine).