title: Traversing a graph (with possible loops) and returning the path in Prolog
body: <p>I'm given a list of arcs:</p>
  
  <pre><code>arc(a,b).
  arc(b,c).
  arc(c,d).
  arc(d,b).
  arc(d,e).
  arc(e,e).
  arc(e,f).
  </code></pre>
  
  <p>I've written a set of clauses which will tell me if there's a path from node <code>X</code> to node <code>Y</code>.  Loops may occur and I've accounted for that.</p>
  
  <pre><code>path(X,Y) :-
      arc(X,Y).
  path(X,Y) :-
      arc(X,Z),
      path(Z,Y,[X]).
  
  path(X,Y,P) :-
      arc(X,Y).
  path(X,Y,P) :-
      \+ member(X,P),
      arc(X,Z),
      append([X],P,L),
      path(Z,Y,L).
  </code></pre>
  
  <p>I need to modify this to, on success, return a list of nodes that were traversed.  I'm unclear as to how I would do this.</p>
  
  <p>I assume my base case would be something like <code>path2(X,Y,[X,Y]) :- arc(X,Y).</code> but that won't work with my program.  Is there something wrong with my solution for the previous part, or am I just missing a small modification?  Any help would be appreciated.  Thanks!</p>