Intersection and union of 2 lists

Also, not sure why you’re dead against cuts, so long as their removal would not change the declaritive meaning of the code, as per your link. For example:

inter([], _, []).

inter([H1|T1], L2, [H1|Res]) :-
    member(H1, L2),
    inter(T1, L2, Res).

inter([_|T1], L2, Res) :-
    inter(T1, L2, Res).

test(X):-
        inter([1,3,5,2,4], [6,1,2], X), !.

test(X).
X = [1, 2].

In the test bit where I call the code, I’m just saying do the intersection but I’m only interested in the first answer. There are no cuts in the predicate definitions themselves.

Leave a Comment