gpt4 book ai didi

Prolog 大于/2 成功

转载 作者:行者123 更新时间:2023-12-02 15:39:02 29 4
gpt4 key购买 nike

我是 Prolog 的新手,我正在尝试解决这个练习:

Define a predicate greater_than/2 that takes two numerals in the notation that we introduced in this lecture (i.e. 0, succ(0), succ(succ(0))...) as arguments and decides whether the first one is greater than the second one. E.g:

?- greater_than( succ(succ(succ(0))), succ(0) ).
yes.
?- greater_than( succ(succ(0)), succ(succ(succ(0))) ).
no.

到目前为止,这是我的答案:

greater_than(X, 0).
greater_than( succ(succ(X)), succ(Y) ).

但是当然不能正常工作,所以我向任何人寻求帮助。谢谢。

最佳答案

在寻找递归解决方案时,您必须提供基本情况和递归步骤。您提供的基本案例几乎是正确的。但是它失败了,因为它会成功,例如当两个数字都为零时。只有当左侧的形式为 succ(_) 且右侧为零时,它才会成功。

递归步骤应该从每一边取一个元素并应用递归。因此这应该有效:

greater_than(succ(_), 0).
greater_than(succ(X), succ(Y)):-
greater_than(X, Y).

关于Prolog 大于/2 成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11485007/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com