user1428237
user1428237

Reputation: 49

control on backtracking

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 ?

Upvotes: 1

Views: 131

Answers (1)

Junuxx
Junuxx

Reputation: 14251

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.

avoidg(X) :-
    member(A,X),
    A \= f,
    A \= g,
    write(A).

avoidg(X):-
    member(A,X),
    A \= f, 
    write(A).

Example:

?- avoidg([f,f,f,g,f,f,f]).
g
true 

 ?- avoidg([f,f,f,g,f,f,h]).
h
true 

Upvotes: 1

Related Questions