The first part of the problem is to get next related node in the graph, this can be done using
suivant_2(X, Y) :- arc(X, Y).
suivant_2(X, Y) :- arc(X, Z), suivant_2(Z, Y).
Example queries
?- suivant_2(a,R).
R = o .
?- suivant_2(l,R).
R = v ;
R = l ;
R = v ;
R = l ;
R = v ;
...
?- suivant_2(v,R).
R = l .
?- suivant_2(y,R).
R = v ;
R = o ;
R = l ;
R = v ;
R = l ;
R = v ;
...
Obviously there needs to be a change to stop the back and forth between l
and v
but that is easy to fix, e.g. member/2. Yes that means the code now needs to capture the values into a list but you learned that in the previous exercise.
Once you have valid results, then collect them into a list with something from Finding all Solutions to a Goal