I want to write a predicate split(List, Pivot, Result)
holds when Result is a list of sublists that List divided by Pivot. For example split([A, B, '#', C, '#', D], '#', [[A, B], [C], [D]])
is true. Note that the elements are lists with logic variables.
I asked a question about how to do this to lists which do not have logic variables as elements. Here's the answer:
split(L,P,R):-split(L,P,[],R).
split([],_,[],[]).
split([],_,S,[S]) :- S \= [].
split([P|T],P,[],R) :- split(T,P,[],R).
split([P|T],P,L,[L|R]) :- L \= [], split(T,P,[],R).
split([H|T],P,S,R) :- H \= P, append(S, [H], S2), split(T,P,S2,R).
However this does not work with the lists which have logic variables as elements. Any suggestions to solve this problem? Thank you!