Reputation:
Getting error in Prolog. I want the 1st list to have positive numbers divisible by 2 and 2nd list to have negative number divisible by 3. This is the code I've written so far but I'm getting the output as 'false'
split([],[],[]).
split([X|L],[X|L1],L2):- X >=0, X mod 2 =:= 0, split(L,L1,L2).
split([X|L],L1,[X|L2]):- X<0, X mod 3 =:= 0, split(L,L1,L2).
test case - split([1,-2,-3,4,-17,3],L1,L2).
output - false
Can someone please tell me where am I going wrong?
Upvotes: 1
Views: 84
Reputation: 476659
You did not specify a clause for values that are not positive and dividable by two, or negative and dividable by three. In that case you probably do not want to include it in any list, so:
split([],[],[]).
split([X|L],[X|L1],L2):-
X >= 0,
X mod 2 =:= 0,
split(L,L1,L2).
split([X|L],L1,[X|L2]):-
X < 0,
X mod 3 =:= 0,
split(L,L1,L2).
split([X|L], L1, L2) :-
\+ ((X >= 0, X mod 2 =:= 0); (X < 0, X mod 3 =:= 0)),
split(L, L1, L2).
Upvotes: 2