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(['_', '_', '#', '_', '#', '_'], '#', [['_','_'], ['_'], ['_']])
is true
.
My code is like this and it doesn't work:
split(List, Pivot, Result) :-
split(List, Pivot, _, _, Result).
split(List, Pivot, Left, Right, X|Xs) :-
append(Left, [Pivot|Right], List),
!,
member(Pivot, Right)
-> X = [Left],
split(Right, Pivot, _, _, Xs)
; X = [[Left]|[Right]].
I don't think my approach is clever either. Can someone give me some advice? Thank you.