Want to print the path,but getting errors

2019-08-10 11:57发布

  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).

Want to print the path which i have followed in going from one point to another.But getting errors.For examples, i want:

goal
solve(a,c,P).

Answer:P=[a,b,c]

2条回答
Bombasti
2楼-- · 2019-08-10 12:17

I don't get what your solve predicate does. Here is my guess about how it should be :

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 will give you the paths one after another using the ; to browse them (well, I guess at least since I don't know anything about the prolog implementation you're using).

查看更多
聊天终结者
3楼-- · 2019-08-10 12:33
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).
查看更多
登录 后发表回答