A clearer problem statement would be nice: it's hard to tell from your question, but it sound to me like you want to match the prefix of a list and get back all the elements that aren't part of the prefix. Here's how I would do that:
match_prefix( [] , [] , [] ).
match_prefix( [] , [Y|Ys] , [Y|Ys] ).
match_prefix( [H|Xs] , [H|Ys] , Tail ) :-
match_prefix( Xs , Ys , Tail ).
- if the 1st argument (the desired prefix) is an empty list, and the 2nd argument (the list to be checked for a prefix) is an empty list, then the 3rd argument (the non-prefix tail of the lists to be checked) is an empty list.
- Otherwise...if the 1st argument is an empty list, but the second argument is a non-empty list, we've exhausted the desired prefix, so we unify the 3rd argument (the result) with the second argument (the list).
- Otherwise...if the 1st 2 arguments are non-empty lists and their heads unify, we strip the heads off and recurse down on the tails.
That should about do it (but I can't say I've tested it, since I don't currently have a prolog to play with).