2
votes

I have a family tree program in Prolog that contain next facts:

male(alex). 
male(david).
male(peter).
etc
...
female(sofia).
etc
....
parent(alex, peter).
parent(sofia, peter).  
etc
....

and some rules such as:

father(X, Y) :- parent(X, Y), male(X).
mother(X, Y) :- parent(X, Y), female(X).
etc
....

I need to find all fathers who has two or more children.
I think that I need to go through all fathers im my program, put their children to list and count it's length, then if >=2 I add this father to another list or just print him and go further.

1

1 Answers

1
votes

Although in the general case when you want to find all fathers with at least N children your approach would be required, a much simpler approach is available when you are dealing with only two children: find a child twice, and see if the two children are different.

In Prolog this rule would look like this:

father_of_two(X) :-
    father(X, A),
    father(X, B),
    A \= B.

All you need to do now is collecting all people who match father_of_two predicate into a list.