Want to print the path,but getting errors

老子叫甜甜 提交于 2019-12-11 12:43:40

问题


  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]


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/8297031/want-to-print-the-path-but-getting-errors

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