domains
list=symbol*
predicates
path(symbol,symbol)
solve(symbol,symbol,list)
insert(symbol,list,list)
clauses
path(a,b).
path(b,c).
path(c,d).
path(d,e).
path(a,d).
path(c,e).
solve(X, Z, P):-
path(X,Z),
insert(Z,Temp,P),
P=Temp.
solve(X,Z,P):-
path(X,Y),
insert(Y,Temp,P),
P=Temp,
solve(Y,Z,P).
insert(X,[X|Tail],Tail).
insert(X,[Y|Tail],[Y|Tail1]):-
insert(X,Tail,Tail1).
想打印我从一个点到另一个点所遵循的路径。但是会出现错误。例如,我想:
goal
solve(a,c,P).
答:p = (a, b, c)
我不明白你的解谓词是做什么的。以下是我的猜测:
solve(Start, End, [Start|PathMinusStart]) :-
solve_(Start, End, PathMinusStart).
solve_(Start, End, [End]) :-
path(Start, End).
solve_(Start, End, [Waypoint|Path]) :-
path(Start, Waypoint),
solve_(Waypoint, End, Path).
solve将使用;
为您提供一个接一个的路径来浏览它们(好吧,我想至少因为我对您正在使用的prolog实现一无所知)。
search_path(Node, End, Temp, Path) :- path(Node,End),reverse([End | [Node | Temp]], Path).
search_path(Node, End, Temp, Path) :-
path(Node, Next),
not(member(Node, Temp)),
search_path(Next, End, [Node | Temp], Path).
solve(Start, End, Path) :- search_path(Start, End, [], Path).