Prolog——在特殊情况下消除重复答案的更好方法?
我在处理这两行时遇到了麻烦:
list_swizzle(L, [], L).
list_swizzle([], L, L).
问题是,如果前两个参数都是空列表,则前两个语句都将被使用,返回相同的答案。然而,如果我在其中插入一个切口,就会破坏回溯。我最终在它们上方添加了这一行:
list_swizzle([], [], []):- !.
并且它有效。但我想知道是否有更优雅的解决方案。
I was having trouble with these two lines:
list_swizzle(L, [], L).
list_swizzle([], L, L).
The problem was that if the both of the first two arguments are the empty list, the first two statements would both be used, returning the same answer. However, if I put a cut in one, it wrecks backtracking. I eventually put in this line above them:
list_swizzle([], [], []):- !.
And it works. But I was wondering if there is a more elegant solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我的版本:
我指望 [] 在第一个事实中不会与 [H | T] 统一。换句话说,[] 没有 T,因为它是空列表,因此第一个事实与第一个参数中带有 [] 的目标不匹配。
我已经在 SWI-Prolog(多线程,32 位,版本 5.8.2)上成功运行了这个
......
Here's my version:
I'm counting on [] not unifying against [H|T] in the first fact. In other words [] has no T because it's the empty list so the first fact doesn't match goals with a [] in the first arg.
I've run this successfully on SWI-Prolog (Multi-threaded, 32 bits, Version 5.8.2)
....