Prolog 列出错误:超出全局堆栈
我正在尝试在序言中写一条规则 adjacent(X,Y,Zs)
,如果 X
和 Y
彼此相邻,则为 true
列出Z
。
我目前有:
append([],L,L).
append([H|T],L,[H|LT]):-append(T,L,LT).
sublist(S,L):-append(_,S,P),append(P,_,L).
adjacent(X,Y,Zs):-sublist([X,Y],Zs).
测试:
1 ?- sublist([1,2],[1,2,3,4]).
true .
2 ?- sublist([1,3],[1,2,3,4,5]).
ERROR: Out of global stack
3 ?-
你们有什么想法吗?提前致谢。
I'm trying to write a rule in prologadjacent(X,Y,Zs)
, as true
if X
and Y
are adjacent to each other in the list Zs
.
I currently have:
append([],L,L).
append([H|T],L,[H|LT]):-append(T,L,LT).
sublist(S,L):-append(_,S,P),append(P,_,L).
adjacent(X,Y,Zs):-sublist([X,Y],Zs).
test:
1 ?- sublist([1,2],[1,2,3,4]).
true .
2 ?- sublist([1,3],[1,2,3,4,5]).
ERROR: Out of global stack
3 ?-
Do you guys have any ideas? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那应该有效。
另外,列表库中有一个名为
nextto(?X, ?Y, ?List)
的谓词,它会执行相同的操作(但请记住,该谓词的语义是在列表中,Y
位于X
之后,而不只是以任何顺序简单相邻)。http://www.swi-prolog.org/pldoc/doc_for?object=section%282,%27A.12%27,swi%28%27/doc/Manual/lists.html%27 %29%29
that should work.
also, you have a predicate in the lists library called
nextto(?X, ?Y, ?List)
that will do the same (but keep in mind that the semantics of this predicate is thatY
followsX
in the list, not just plain adjacent in any order).http://www.swi-prolog.org/pldoc/doc_for?object=section%282,%27A.12%27,swi%28%27/doc/Manual/lists.html%27%29%29
使用 DCG 将以最图形化的方式表示问题:
编辑:感谢 @fortran 的评论,另一个定义可能是:
Using DCGs will represent the problem in the most graphical way possible:
Edit: Thanks to @fortran's comment, another definition might be:
你的程序的行为相当复杂。该错误位于 sublist/2 中。
为了跟踪,我已重命名谓词,在名称中添加 1,但定义是从代码中逐字获取的。
您可以看到对append1(标记为9,10,11,...)的调用逐渐扩展未绑定的第一个参数。
不管怎样,我认为你学习Prolog的道路是正确的。掌握内置函数的用法是正确的,看似简单的定义背后往往隐藏着复杂的行为。
The behaviour of your program it's rather complex. The bug is in the sublist/2.
To trace I've renamed the predicates adding 1 to the names, but the definition it's taken verbatim from your code.
You can see that there are calls to append1 (labelled 9,10,11,...) that progressively expand the unbound first argument.
Anyway, I think that you have taken the right path to learn Prolog. It's correct to master usage of builtins, that behind deceptively simple definition often hide complex behaviour.