'Intersection in SWI-Prolog

I am new to Prolog and I am having a little bit of trouble understanding recursion. I am trying to write a relation that finds the intersection of two sorted lists without using SWI's built-in intersect. I've used trace to see what's happening, and it's behaving as I expect up until the point where I want it to terminate and return the new list that contains the intersection. This makes me think that my base case is wrong. I've played around with several different ways of forming the base case, but it hasn't been fruitful. I've been using the lists [1, 2, 3, 4] and [2, 4, 6] as test cases with the following relations (the base case on top is just one I threw in as a placeholder... it doesn't work at all):

intersectS([], [], []).
intersectS([A | B], [C | D], Z) :- A < C, intersectS(B, [C | D], Z).
intersectS([A | B], [C | D], Z) :- A > C, intersectS([A | B], D, Z).
intersectS([A | B], [C | D], Z) :- A = C, append(Z, [A], Y), intersectS(B, D, Y). 

Any help is appreciated. I've seen examples where the cut (!) operator is used alongside the member/non-member, but I'm supposed to take advantage of the fact that the lists are sorted so I thought I would try this approach. Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source