Next: , Previous: , Up: Programming Examples   [Contents][Index]


9.11.2 Family Example (descendants)

The goal descendant(X,Y) is true if Y is a descendant of X.

descendant(X, Y) :- offspring(X, Y).
descendant(X, Z) :- offspring(X, Y), descendant(Y, Z).

offspring(abraham, ishmael).
offspring(abraham, isaac).
offspring(isaac, esau).
offspring(isaac, jacob).

If for example the query

| ?- descendant(abraham, X).

is executed, then Prolog’s backtracking results in different descendants of Abraham being returned as successive instances of the variable X, i.e.

X = ishmael
X = isaac
X = esau
X = jacob

Send feedback on this subject.