prolognon-terminationclpfd

Trying to count steps through recursion?


cube

This is a cube, the edges of which are directional; It can only go left to right, back to front and top to bottom.

edge(a,b).
edge(a,c).
edge(a,e).
edge(b,d).
edge(b,f).
edge(c,d).
edge(c,g).
edge(d,h).
edge(e,f).
edge(e,g).
edge(f,h).
edge(g,h).

With the method below we can check if we can go from A-H for example: cango(A,H).

move(X,Y):- edge(X,Y).
move(X,Y):- edge(X,Z), move(Z,Y).

With move2, I'm trying to impalement counting of steps required.

move2(X,Y,N):- N is N+1, edge(X,Y).
move2(X,Y,N):- N is N+1, edge(X,Z), move2(Z,Y,N).

How would I implement this?


Solution

  • move2(X,Y,1):- edge(X,Y), ! .
    move2(X,Y,NN):- edge(X,Z), move2(Z,Y,N), NN is N+1 .