user14545805
user14545805

Reputation: 37

Separating list of facts in prolog

I have a list of facts in Prolog. Say:

L = [mother(sue), father(sam), mother(rachel), mother(izzie), father(leo)] 

I want to separate them into two lists

L1 = [mother(sue), mother(rachel), mother(izzie)]
L2 = [father(sam), father(leo)]

How do I do that in Prolog

Upvotes: 1

Views: 47

Answers (1)

Christian Fritz
Christian Fritz

Reputation: 21354

I hope I'm not doing your homework for you, but here it is:

split([], [], []).
split([mother(X)|L], [mother(X)|L1], L2) :- split(L, L1, L2).
split([father(X)|L], L1, [father(X)|L2]) :- split(L, L1, L2).

Usage:

?- split([mother(sue), father(sam), mother(rachel), mother(izzie), father(leo)], L1, L2).
L1 = [mother(sue), mother(rachel), mother(izzie)],
L2 = [father(sam), father(leo)]

Upvotes: 2

Related Questions