Prolog and ancestor relationship -


I have to write a small program that checks that if a person is the ancestor of another person, these are the facts and the rules: Mother (Tim, Anna) Father (Daniel, Fanny) Mother (Daniel, Fanny) Father (Celine, Gertrude) Father (Tim, Berland) Father (Anna, Ephraim) Father (Daniel, Parents (X, Y) Parents (X, Y) Parents (X, Y): - Father (X, Y)

Fairy If a person is the ancestor of another person:

  Ancestor (X, Y): - Parent (X, Y). Ancestor (X, Y): - Parent (X, Z), Ancestor (Z, Y)  

But now I have to write a method ancestor (x, y, z) Also prints the relationship between It should look like this

 ? - Ancestor (Ephraim, Tim, X). false. ? - Ancestor (Tim, Ephraim, X). X = father (mother) (Tim)) and this is the problem: I have no clue how to do it. 

You can use an accelerator to suit @ Scott Hunter's solution:

  Mother (Anna, Fanny). Father (Daniel, Fanny) Father (Celine, Gertrude) Father (Tim, Berrand) Father (Anna, Euphr.) Father (Daniel, Epaphras) Father (Seine, Daniel) Ancestor (X, Y, Z): - Ancestor (X, Y, X, Z). Ancestor (X, Y, ACC, Father): - Father (X, Y) ancestor (X, Y, ACC, mother): - Mother (X, Y). Ancestor (x, y, acc, result): - father (x, z), ancestor (z, y, father, result), results. Ancestor (x, y, acc, result): - mother (x, z), ancestor (z, y, mother, acc, result). Edit: As Scott Hunter showed in his edit, there is no need for a clear accumulator here, because we can easily leave the unbound portion of the word on each iteration. You can. His solution is better! 


Comments