An alternative way of thinking about the problem is to define a “mathematical” specification of what it means for one list to be a sublist of another.
For example, A is a sublist of B if B can be broken into a “before”, “middle”, and “after” (any of which can be empty), and A is equal to one of these. In Python, I can test this as follows:
>>> a = [2,3,4]
>>> b = [1,2,3,4,5,6]
>>> before = [1]
>>> after = [5,6]
>>> before + a + after == b # tests: "a is sublist of b"
True
>>> b = [2,3,4,5,6]
>>> before = []
>>> after = [5,6]
>>> before + a + after == b # tests: "a is sublist of b"
True
So: First I compare the first element of the first list and the second list. If they are equal, I do the same with the restlist. If it isn´t the same I still take the first element of the first list and compare it to the rest oft the second list. It ends if the first list ist empty. And gives out true if the first list is empty and the second list or the second list is still filled with elements.
If we read foo1(X,Y) as “X is a subset of Y”, then the 1st clause means “the empty list is a subset of a list containing one element” and the 2nd clause means “the empty list is a subset of the empty list”.
You probably want to say something different: “the empty list is a subset of …”.
(Your last clause can be read “X is a subset of Y if the first element of X is the same as the first element of Y and the rest of X is a subset of the rest of Y; or if X is a subset of the rest of Y”.)
The easy solution is probably to use append/3. From the examples it seems you really want a sublist (not a subsequence). As Peter above tried to explain:
Thank you very much for your help. I´m much smarter now.
I figured out, that my code is working if I implement an unattractive cut. Like that:
foo1([],[RL2]) :- !.
foo1([],[]).
foo1([E|RL], [E2|RL2]) :-
E = E2 ->
foo1(RL,RL2);
foo1([E|RL],RL2).