Here's the question I'm trying to solve:
Write and test the clauses for the relation: listForPath(X,Y,L) %the path between X and Y is the list L.
Here are the graphs we're working with:
edge(a,b,2).
edge(b,c,2).
edge(c,d,2).
edge(d,a,2).
as well as
edge(a, c, 2).
edge(b, e, 2).
edge(d, e, 1).
edge(c, d, 2).
edge(a, b, 2).
edge(a, d, 3).
edge(b, a, 1).
edge(a, a, 2).
our expected output is as follows:
?- listForPath(a,d,L).
L = [edge(a,b,2),edge(b,c,2),edge(c,d,2)] ;
L = [edge(a,b,2),edge(b,c,2),edge(c,d,2),edge(d,a,2),edge(a,b,2),edge(b,c,2),edge(c,d,2)] ;
I've been playing around with this for a while and here's what I have:
listForPath(X,Y,L):-
edge(X,Y,Z),
L=[edge(X,Y,Z)].
listForPath(X,Y,L):-
edge(X,Z,W),
listForPath(Z,Y,L2),
append([edge(X,Z,W)],L2,L).
From this, I'm getting this output:
89 ?- listForPath(a,d,L).
L = [edge(a, d, 3)] ;
L = [edge(a, b, 2), edge(b, c, 2), edge(c, d, 2)] ;
L = [edge(a, b, 2), edge(b, c, 2), edge(c, d, 2)] ;
L = [edge(a, b, 2), edge(b, c, 2), edge(c, d, 2), edge(d, a, 2), edge(a, d, 3)] ;
L = [edge(a, b, 2), edge(b, c, 2), edge(c, d, 2), edge(d, a, 2), edge(a, b, 2), edge(b, c, 2), edge(c, d, 2)] ;
L = [edge(a, b, 2), edge(b, c, 2), edge(c, d, 2), edge(d, a, 2), edge(a, b, 2), edge(b, c, 2), edge(c, d, 2)] ;
L = [edge(a, b, 2), edge(b, c, 2), edge(c, d, 2), edge(d, a, 2), edge(a, b, 2), edge(b, c, 2), edge(c, d, 2), edge(d, a, 2), edge(..., ..., ...)] .
So, I think I'm close as I'm getting the something that resembles the desired output, but it appears as though I'm getting stuck in the loop and I can't get out! Can anyone help. Please?