My objective is to be able to traverse nodes in a diagram and output the length of the path in Prolog. I'm using recursion and I'm stuck on this issue. Here's my attempt so far.
edge(a,b).
edge(b,c).
edge(a,d).
edge(d,f).
distance(X,Y,1) :- edge(X,Y).
distance(X,Y,Dis) :- edge(X,Z), distance(Z, Y, D), Dis is D +1.
Issue: I'd like to be able to say Dis = 0. if the path is invalid. As in, there is no edge connecting the two nodes, Dis = 0. Currently my code says false for an invalid path. My attempts in this endeavor have lead to me breaking the recursion. Thanks for any help.