In my prolog rule
marriedcouple(X,Y) :-
parent( (X,Z), bornin(_) ),
parent( (Y,Z), bornin(_) ),
female(X),
male(Y)
;
male(X),
female(Y),
different(X,Y).
when a parent has two kids, the couple shows twice. How can we prevent this ?
Just an theoretical solution through double-not:
Given that you've got
female/1
&male/1
predicates the predicate becomes quite simple.However, if you want to see if X and Y are not the same use the
(\==)/2
operator for "not identical" or(\=)/2
for "not unifiable".In order to prevent the same answer coming back twice there's a number of choices. We can build a list of solutions and only add a newly found solution if it isn't already in the list. Or use an approach that incorporates state using the
assert/1
predicate.I've chosen the latter.
When I run this I get the following:
Be careful using
assert/1
predicates as you may introduce unwanted side-effects into your programs. You may need to do appropriateretract/1
calls too.