Warning, I'm quite new to Prolog.
I've written a split predicate in Prolog. It splits a list into two new lists. One that contains items greater than Key, and one that contains items less than or equal to Key. It should only ever return one set of answers. The problem is if I type ; to check for more answers, it keeps giving me the answer I already got and then eventually the listener crashes. I was wondering if you could help me fix this?
Code:
split([],_,[],[]).
split([H|T],Key,Small,Big):-
H=<Key,
removeFirst(Small,H,NewSmall),
split(T,Key,NewSmall,Big).
split([H|T],Key,Small,Big):-
H>Key,
removeFirst(Big,H,NewBig),
split(T,Key,Small,NewBig).
removeFirst([H|T],H,T).
removeFirst(L,Key,Result):-
divide(L,Key,F,E),
X = F,
Y = E,
append(X,Y,Z),
Result = Z.
Output:
?- split([1,2,3,4,5],3,S,B).
S = [1, 2, 3]
B = [4, 5] ;
S = [1, 2, 3]
B = [4, 5] ;
S = [1, 2, 3]
B = [4, 5] ;
Listener crashes on 4th attempt.