Connected metro stations in prolog

青春壹個敷衍的年華 提交于 2020-01-06 06:56:37

问题


I have this problem of connected stations

connected(ataba,naguib).
connected(naguib,sadat).
connected(sadat,opera).
connected(opera,dokki).

and i should show the full path taken by the metro, from a source station to a destination

path(ataba,dokki,Z). 
Z = [[ataba, naguib], [naguib, sadat], [sadat, opera], 
[opera, dokki]] . 

i tried to do this :

addlast(X,[],[X]).
addlast(X,[H|T],[H,NewT]):-
addlast(X,T,NewT).

path(X,Y,L) :- connected(X,Y),addlast([X|Y],L,R).

path(X,Y,L):-
    connected(X,Z),
    addlast([X,Z],L,R),
    path(Z,Y,R).

when i tried to trace i noticed i have a problem with the stopping condition , i dont know how to solve it ,any advice or insights would be very helpful.

来源:https://stackoverflow.com/questions/49474648/connected-metro-stations-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!