10. write_descendant_chain. Later chapters examine the special predicates write and nl, which can be used to produce...
Question:
10. write_descendant_chain.
Later chapters examine the special predicates write and nl, which can be used to produce output other than just the values of variables. For example, if there is a clause write_child(X,Y) :-
write(X), write(’ is a child of ’), write(Y), nl.
then the query write_child(john_smith,sue_jones) will always succeed and will also print john_smith is a child of sue_jones on one line. Include this predicate in your program, and write the clauses for a predicate write_descendant_chain(x, y) that prints a chain from x to y when x is a descendant of y but prints nothing when x is not a descendant of y. For example, if John Smith is a descendant of William Brown, then write_descendant_chain(john_smith,william_brown) should print a sequence of lines something like this:
john_smith is a child of sue_jones sue_jones is a child of harvey_jones harvey_jones is a child of davy_jones davy_jones is a child of anna_brown anna_brown is a child of william_brownHint: You will not be able to use the descendant predicate you defined for this.
Use the child predicate together with write_child. Also, work down from y rather than up from x.
In case you were wondering, these questions do not exhaust the varieties of cousins people have considered. Anthropologists distinguish between parallel and cross first cousins, according to whether or not the two parents who are siblings are of the same sex. And once the idea of people being married is included, there are new possibilities:
step-siblings (children of the spouse of a parent who are not siblings) and siblings-inlaw in two varieties (a sibling of a spouse and the spouse of a sibling). This lead to step first cousins and two varieties of first cousins-in-law. And so on.
Step by Step Answer: