Re: Tracing with out using trace?



Bruce Horrocks <news@xxxxxxxxxxxxxxxxx> writes:

Or friend-of-friend where there could be any number of connections
before getting to a final person?

What about it? A list can hold any number of connections. Example
fact base and rules for (transitive) friend-of-friend:

friend(mary, harry).
friend(harry, sally).
friend(sally, garry).
friend(harry, mary).
friend(garry, sally).

friend_of_a_friend([A,B|Rest]) :-
friend(A, B),
friend_of_a_friend(B, Rest, [A,B]).

friend_of_a_friend(A, [B], Visited) :-
friend(A, B),
\+ memberchk(B, Visited).
friend_of_a_friend(A, [B|Rest], Visited) :-
friend(A, B),
\+ memberchk(B, Visited),
friend_of_a_friend(B, Rest, [B|Visited]).

Yielding:

?- friend_of_a_friend(Fs).

Fs = [mary, harry, sally] ;

Fs = [mary, harry, sally, garry] ;

Fs = [harry, sally, garry] ;


It's easy to derive the corresponding binary relation.

All the best,
-- Markus Triska
.