0

We can carry a count with us as we move, this works for movesto(a,e, Dist) but crashes my GNU-Prolog for impossible case movesto(c,e), I do not know what should be changed:

```
/*--------------
connections between points
--------------*/
link(a,b).
link(b,c).
link(b,d).
link(d,e).
/*--------------
logic carried out to connect the movements.
--------------*/
movesto(A,B,1):- link(A,B).
movesto(A,B,2):- link(A,C), link(C,B).
movesto(A,B,Dist):- movesto(A,C,Dist1), movesto(C,B,Dist2), Dist is Dist1 + Dist2.
```

1

The first statement must be link to reduce search:

```
/*--------------
connections between points
--------------*/
link(a,b).
link(b,c).
link(b,d).
link(d,e). link(c,f). link(f,h).
/*--------------
logic carried out to connect the movements.
--------------*/
movesto(A,B,1):- link(A,B).
movesto(A,B,Dist):- link(A,C), movesto(C,B,Dist1), Dist is Dist1 + 1.
```

```
| ?- movesto(b,X,D).
D = 1
X = c ? ;
D = 1
X = d ? ;
D = 2
X = f ? ;
D = 3
X = h ? ;
D = 2
X = e ? ;
(31 ms) no
| ?-
```

Question Answered as of 2 Years Ago by
pyTony

You

This question has already been solved: Start a new discussion instead