Assume that backtracking is done as shown below. Each end point show success or fail.
Ex:
foo(X, search_key).
Backtracking :
Root
/ | \
/ | \
/ | \
/|\ | \
/ | \ | /|\
/ f f | / | \
/ | f | f
f g f
Abbreviation f : fail
g show first character of name
Unless no other way is found, I will not prefer to use g as argument in my next function.However, in this example, since no other way is found, I must use g as an argument in my next function.
How can I do that ?
You’ll have to try all end points first and store them in a list (with
findall(foo...)presumably), otherwise you won’t know whether a non-g occurs later.Then try to do the next function with the requirement that X is not g. If that fails, use g anyway.
Example: