Prolog 中的堆栈溢出
我正在尝试编写 Prolog 代码来确定绑定变量 X
是否在列表中绑定变量 Y
的范围内。列表可以嵌套,并且如果 X
和 Y
是同一列表的成员,则 X
位于 Y
的范围内或者如果X
是一个列表的成员,该列表是一个列表的成员,该列表是一个列表的成员...(无限嵌套)与Y.这里我定义
in_scope(X,Y,List)
表示X
在最外层列表List中
。我编写了以下代码,但此代码会导致堆栈溢出:Y
的范围内
in_scope(X,Y,List) :- in(Parent,List), member(X,Parent), member(Y,Parent).
in_scope(X,Y,List) :- in(X,Parent), in_scope(Parent,Y,List).
in(X,Y) :- member(X,Y).
in(X,Y) :- member(X,Z), in(Z,Y).
我希望能帮助您修改代码以避免堆栈溢出。
I am trying to write Prolog code to determine whether the bound variable X
is in the scope of the bound variable Y
in a list. Lists may be nested and X
is in the scope of Y
if X
and Y
are members of the same list or if X
is a member of a list that is a member of a list that is a member of a list...(nested indefinitely) that is in the same list as Y
. Here I define in_scope(X,Y,List)
to mean that X
is in the scope of Y
in the outermost list List
. I have written the following code, but this code results in a stack overflow:
in_scope(X,Y,List) :- in(Parent,List), member(X,Parent), member(Y,Parent).
in_scope(X,Y,List) :- in(X,Parent), in_scope(Parent,Y,List).
in(X,Y) :- member(X,Y).
in(X,Y) :- member(X,Z), in(Z,Y).
I would appreciate help in modifying the code to avoid the stack overflow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我懒得追踪实际的错误,但下面的简化代码
给出了预期的结果:
但请注意以下几点;我不确定这是否是预期行为:
I was too lazy to trace the actual error, but the following, simplified code
gives the intended results:
But note the following; I'm not sure if this is intended behavior: