I'm new to Prolog. I'm trying to write a query to check if a person is the friend or friend of friend of another person.
I have some facts like this:
friends(joe, [john, ann, pete, ellen,
maria, jose, bruno, ali, jing, yang]).
friends(john, [rick]).
friends(ellen, [mia, xing, jun, maria]).
friends(maria, [pete, ellen, zhang, jose, serena]).
friends(serena, [ali, rick, zhang, mia]).
friends(jose, [maria, jose, bruno, ali, jing]).
My target is to write a query like this:
visible(joe, ann).
true
I have made something like this:
visible(X,Y) :- friends(X,[Y|T]); member(Y,T).
visible(X,Y) :- visible(X,Z), visible(Z,Y).
member(X,[X|T]).
member(X,[H|T]) :- member(X,T).
But it becomes an infinite loop. I don't know how to write the base case.
The relationship is a graph with loop. Is there anyway to recursively find the friend of friend, transitively?