Prolog 中的简单 nth1 谓词
在 SWI Prolog 中,有一个谓词可以查找名为 nth1 的列表中的第 n 个项目。我想实现我自己的谓词版本,但如果您查看清单(nth1)代码,SWI 的版本会非常复杂。有更简单的方法吗?
谢谢 :)。
With SWI Prolog, there's a predicate that finds the nth item in a list called nth1. I want to implement my own version of the predicate but SWI's is so complicated if you look at the listing(nth1) code. Is there a simpler way of doing it?
Thank you :).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑对一般(可逆)整数运算使用有限域约束:
Consider using finite domain constraints for general (reversible) integer arithmetic:
我并不是故意要自相矛盾或者让别人来真正做我的工作;我只是想要一些建议,抱歉没有说得更清楚。
我现在已经自己实现了它,但是你们能建议改进或更好的方法吗?我经常发现自己在 Prolog 中做的事情是编写一个带有一个或一组计数器的谓词,并使用较少参数的谓词来调用带有额外参数的子句。这通常最终会产生相当多的代码。不管怎样,这是我刚刚完成的实现:
有什么意见吗?谢谢 :)。用法:
I didn't mean to be contradictory or get someone else to do my work actually; I just wanted some advice, sorry for not being clearer.
I've implemented it myself now but could you guys possibly suggest improvements or better ways of doing it? What I often find myself doing in Prolog is writing a predicate with say a counter or set of counters and getting a predicate with fewer arguments to call the clauses with extra arguments. This often ends up producing quite a bit of code. Anyway, here's my implementation I just did:
Any comments? Thanks :). Usage:
SWI 代码有点复杂,因为谓词可用于从变量索引生成:
如果您不想要这种行为,可以根据
nth0 轻松实现
:nth1/3
编辑:也可以不用
nth0
只需几行代码即可完成:The SWI code is a bit complex because the predicate can be used to generate from a variable index:
If you don't want that behavior,
nth1/3
can be implemented easily in terms ofnth0
:Edit: it's also possible to do without
nth0
in just a few lines of code: