title: Prolog, find friend of friend
body: <p>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.</p>
  
  <p>I have some facts like this:</p>
  
  <pre><code>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]).
  </code></pre>
  
  <p>My target is to write a query like this:</p>
  
  <pre><code>visible(joe, ann).
  true
  </code></pre>
  
  <p>I have made something like this:</p>
  
  <pre><code>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).
  </code></pre>
  
  <p>But it becomes an infinite loop. I don't know how to write the base case.</p>
  
  <p>The relationship is a graph with loop. Is there anyway to recursively find the friend of friend, transitively?</p>