-3

I need a code in prolog.

Assume we defined some persons in family tree.

How can I write a function that get two persons name and process if they have same generation and who is their same ancestor?

parent(chester,irvin).
parent(chester,clarence).
parent(chester,mildred).
parent(irvin,ron).
parent(irvin,ken).
parent(clarence,shirley).
parent(clarence,sharon).
parent(clarence,charlie).
parent(mildred,mary).


male(chester).
female(mildred).
male(irvin).
female(shirley).
male(clarence).
female(sharon).
male(ron).
female(mary).
male(ken).
male(charlie).



father(X,Y) :- parent(X,Y), male(X).

mother(X,Y) :- parent(X,Y), female(X).

grandparent(X,Y) :- parent(X,Z), parent(Z,Y).

paternalgrandfather(X,Y) :- father(X,Z), father(Z,Y).

sibling(X,Y) :- parent(Z,X), parent(Z,Y).

brothers(X,Y) :- sibling(X,Y),male(X),male(Y), \+ (X=Y).

samegeneration(x,y) :- HERE I DONT KNOW WHAT TO DO
karthik
  • 17,453
  • 70
  • 78
  • 122
user seven
  • 1
  • 1
  • 2
  • 1
    Sounds like homework? If so, please say so. – Cocowalla Jan 02 '12 at 10:02
  • yes, i wrote family tree and function for finding uncle and grandfather , but my same generation function goes to infinite loop, i tried but i cant fix it! :( – user seven Jan 02 '12 at 10:05
  • it goes to infinite loop ? That for sure means that you wrote something, please post that. – m09 Jan 02 '12 at 18:45

1 Answers1

0

Break it down: start with a predicate for two people having a common ancestor, where having the same parent would be your base case. Then enhance that to keep track or how many generations from that common ancestor each person is. One more step (which is left as an exercise), and you're done!

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101