Prolog、三角数、累加器和尾递归
我正在做一项家庭作业,由两部分组成。 第一个是编写一个 Prolog 程序,检查某个对 X, Y 是否属于 http://en .wikipedia.org/wiki/Triangle_number。例如:(2, 3) = true; (4, 10) = true 等等。
第一个解决方案使用“普通”递归,我已经这样解决了这个问题:
triangle(0, 0).
triangle(X, Y) :- X > 0, Y > 0, A is X - 1, B is Y - X, triangle(A, B).
第二部分是使用尾递归/累加器,使用三角形/3谓词来解决这个问题。 虽然我在另一个作业中使用过累加器,其中的用法非常明显,所以我对如何使用累加器有一个大致的了解,但我对如何在这种情况下使用它感到非常困惑。
所以,我不是在寻找算法,我宁愿自己解决这个问题,而是更多关于如何在这种情况下应用累加器的实用建议。
I'm working on a homework assignment, consisting of 2 parts.
The first is to write a Prolog program that checks if a certain pair X, Y belongs to a http://en.wikipedia.org/wiki/Triangular_number. For example: (2, 3) = true; (4, 10) = true et cetera.
The first solution uses 'ordinary' recursion, and I've solved this like this:
triangle(0, 0).
triangle(X, Y) :- X > 0, Y > 0, A is X - 1, B is Y - X, triangle(A, B).
The second part is to solve this using tail recursion/an accumulator, using a triangle/3 predicate.
Though I have used an accumulator in another assigment, in which the use was quite obvious, so I have a general idea of how to use an accumulator, I'm quite puzzeled as to how to use it in this context.
So, I'm not looking for an algorithm, I'd much rather solve that myself, but more of a practical advise on how to apply an accumulator in this context.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
开头始终相同,即前三行基本上是您为每个尾递归谓词编写的内容(对于列表谓词,使用
[]
而不是0
)。从这里开始,您可以继续进行,无需进行太多更改:
以下是大 X 的一些统计数据:
因此,虽然您自己的谓词基本上已经是尾递归(因为递归调用是最后要做的事情),但带有累加器的版本仍然节省了一些时间因为您不需要检查
Y > 0 。如果您在
triangle_t
谓词中引入这一行,它们将再次具有完全相同的运行时特征:另请注意,您现在可以使用谓词来生成第 n 个三角形数。
The beginning is always the same, i.e. the first three lines are basically what you write for each and every tail recursive predicate (with a
[]
instead of a0
for list predicates).From there you can go on without many changes:
Here are some statistics for large X:
So, while your own predicate is basically already tail recursive (because the recursive call is the last thing to do) the version with an accumulator still saves some time because you do not need the check for
Y > 0
. If you introduce this line in thetriangle_t
predicate, they have exactly the same runtime characteristics again:Also note that you can now use the predicate to generate the n'th triangular number.