tags:

views:

113

answers:

2

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 ?

+2  A: 

Given that you've got female\1 & male\1 predicates the predicate becomes quite simple.

marriedcouple(X,Y) :-
    parent((X,Z),bornin(_)),
    parent((Y,Z),bornin(_)),
    female(X),male(Y).

However, if you want to see if X and Y are not the same use the \== operator for "not identical" or \= for "not unifiable".


Pradeep, based on your comment below, here is a more complete solution.

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.

?- solve.

solve :-
    marriedcouple(Dad, Mum),
    not(found(marriedcouple(Dad, Mum))),
    assert(found(marriedcouple(Dad, Mum))),
    write([Dad, Mum]),
    nl,
    fail.

marriedcouple(Dad, Mum) :-
    parent(Dad, Child),
    parent(Mum, Child),
    male(Dad),
    female(Mum).

male(aaron).
male(adam).

female(betty).
female(eve).

parent(aaron, callum).
parent(aaron, david).
parent(adam, abel).
parent(adam, cain).
parent(betty, callum).
parent(betty, david).
parent(eve, abel).
parent(eve, cain).

When I run this I get the following:

[aaron,betty]
[adam,eve]
No.

Be careful using assert\1 predicates as you may introduce unwanted side-effects into your programs. You may need to do appropriate retract\1 calls too.

Enigmativity
Oh, and you had a semi-colon in your original predicate. That is likely to be a mistake. Cheers.
Enigmativity
Semicolon is there to check whether X,Y is male or female. so its not the issue. from my problem the result i get is X=tomY=annX=tomY=annX=jackY=janei get two result because in that family they have two children. this is i want to stop. please can soemone tell me how to do it
Nubkadiya
The semi-colon will only apply to the male(Y) male(X) ) ,female(Y)," and not ", ( female(X),male(Y) ) ; ( male(X),female(Y) ) ," which is what I think you meant.
Enigmativity
typo: male\1 -> male/1, etc.
Kaarel
@Kaarel - Thanks. I can never remember which way it goes. Might have to blame Windows vs Unix path separators. ;-)
Enigmativity
+1  A: 

Just an theoretical solution through double-not:

marriedcouple(Dad, Mum) :-
    male(Dad), female(Mum),
    not(notMarriedcouple(Dad,Mum)).

notMarriedcouple(Dad, Mum) :-
    male(Dad), female(Mum),
    not((parent(Dad, Child), parent(Mum, Child))).
ony
BTW, there is some systems (XSB) that supports tabulation ":-table marriedcouple/2."
ony