Predicate/3 to split a list of lists, into the last list element of the list and the remainding lists

selector(Path, [Path|RestPaths], RestPaths):-append(RestPaths,Path,_).

It is working for getting the last element of the list. But it is not storing RestPaths.

If possible I want to avoid using append.

[Path|RestPaths] means that Path is the first element.

To split the list into WithoutLast and Last, can use:

list_without_last([Head|Tail], WithoutLast, Last) :-
    list_without_last_(Tail, Head, WithoutLast, Last).

list_without_last_([], Last, [], Last).
list_without_last_([Head|Tail], Prev, [Prev|List], Last) :-
    list_without_last_(Tail, Head, List, Last).
?- list_without_last([a, b, c, d], WO, La).
WO = [a,b,c],
La = d.
1 Like

Thank you for your answer - I will try and rewrite it to fit my original predicate order as it does not currently work with the rest of my program.