Node:Family Example, Next:, Previous:Simple List, Up:Programming Examples



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, 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