Prolog-递归找到所需的解决方案后返回初始状态
我是 Prolog 新手,我无法弄清楚我的谓词出了什么问题。我正在尝试编写一个谓词来迭代顶点列表,例如 [a,b,d,f,k],并返回 edge(X, vertex1, vertex2) 中描述的活动。列表 [a,b,d,f,k] 的期望结果将是 [jump,jump,fly,wow]。
因为边绑定在两个顶点之间,所以我创建了一个谓词 call_act/2 来获取列表的前两项。 call_act/2 向 act/3 提供完整列表(第二项),然后等待结果。 act/3 查找列表中第一项和第二项之间的边,然后将其附加到结果中。
edge(jump,a,b).
edge(run,a,c).
edge(jump,b,d).
edge(fly,d,f).
edge(jump,b,e).
edge(fly,c,f).
edge(run,c,g).
edge(go,d,h).
edge(sprint,e,i).
edge(flip,e,j).
edge(wow,f,k).
call_act([_|[]], _).
call_act([H,H2|T], Result) :-
act([H,H2|T], H2, Result).
act(_,[],[_|_]).
act([H|T], Next, Result) :-
findall(X, edge(X,H,Next), Result1),
append(Result, Result1, ResultN),
write(ResultN),
nl,
call_act(T, ResultN).
如果您像这样运行代码,它将找到正确的解决方案,但之后它会返回到一个空列表。 像这样。
我尝试了一些方法。当 ResultN 的长度为完整列表 -1 时,我尝试停止递归,但这似乎不起作用。我尝试制作 act/3 的修改版本,其中包含最终顶点作为参数,希望我可以制作像 act([FinalVertex|_],_,_,FinalVertex).< /代码>。但我还没有想出解决办法。
我还编写了该代码的一个版本,将两个谓词合并为一个:
act([_|[]],_).
act([H,H2|T], Result) :-
findall(X, edge(X,H,H2), Result1),
append(Result, Result1, ResultN),
write(ResultN),
nl,
act([H2|T], ResultN).
但这有同样的问题。
I'm new to Prolog and I can't figure out what's wrong with my predicates. I'm trying to write a predicate that iterates over a list of vertices, like [a,b,d,f,k], and return the activities described in edge(X, vertex1, vertex2). The desired result for list [a,b,d,f,k] would be [jump, jump, fly, wow].
Because edges are bound between two vertices, I made a predicate call_act/2 that takes the first two items of a list. call_act/2 gives act/3 the full list, the second item and then awaits the result.
act/3 looks for an edge between the first item in the list and the second item and then appends it to the result.
edge(jump,a,b).
edge(run,a,c).
edge(jump,b,d).
edge(fly,d,f).
edge(jump,b,e).
edge(fly,c,f).
edge(run,c,g).
edge(go,d,h).
edge(sprint,e,i).
edge(flip,e,j).
edge(wow,f,k).
call_act([_|[]], _).
call_act([H,H2|T], Result) :-
act([H,H2|T], H2, Result).
act(_,[],[_|_]).
act([H|T], Next, Result) :-
findall(X, edge(X,H,Next), Result1),
append(Result, Result1, ResultN),
write(ResultN),
nl,
call_act(T, ResultN).
If you run the code like this, it will find the correct solution, but afterwards it goes back to an empty list. Like this.
I have tried a couple things. I tried to stop the recursion when ResultN has a length of the full list -1, but this did not seem to work. I've tried making a modified version of act/3 that includes the final vertex as a parameter, in hopes that I could make a base case like act([FinalVertex|_],_,_,FinalVertex).
. But I've not figured out a solution yet.
I've also written a version of this code that merges the two predicates into one:
act([_|[]],_).
act([H,H2|T], Result) :-
findall(X, edge(X,H,H2), Result1),
append(Result, Result1, ResultN),
write(ResultN),
nl,
act([H2|T], ResultN).
But this has the same issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试执行以下操作:
示例:
Try doing the following:
Example: