0

So i have a database of train stations simply labelled train(st1, st4). etc

I want to implement a find all predicate so that a user could query allpaths(X,Y, Paths). And the code would return all paths from X to Y.

Any help would be awesome thanks

John
  • 3
  • 1

2 Answers2

0
train(st1,st4).
train(st1,st2).
train(st2,st3).
train(st3,st4).
train(st4,st5).
train(st3,st6).
train(st6,st5).
find(X,Y,[X|Y]):-train(X,Y).
find(X,Y,[X|Q]):-train(X,Z),find(Z,Y,Q).
findall2(X,Y):-find(X,Y,Q),write(Q),nl,fail.

I hope it's what you were looking for.

sowa
  • 56
  • 5
0

Same answer as @sowa, but with the last line changed to:

allpaths(X, Y, Ps) :- findall(P, find(X, Y, P), Ps).

findall/3 is a built-in Prolog predicate.

Little Bobby Tables
  • 5,261
  • 2
  • 39
  • 49