想打印路径,但出现错误 [英] Want to print the path,but getting errors
本文介绍了想打印路径,但出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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).
答案:P=[a,b,c]
Answer:P=[a,b,c]
推荐答案
我不明白你的解决谓词的作用.这是我对它应该如何的猜测:
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 将使用 ;
依次为您提供路径来浏览它们(好吧,我想至少因为我对您正在使用的 prolog 实现一无所知).
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).
这篇关于想打印路径,但出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文