gpt4 book ai didi

list - Prolog IntList 定义

转载 作者:行者123 更新时间:2023-12-02 00:43:36 24 4
gpt4 key购买 nike

hill(+IntList) succeeds if IntList consists of monotonically increasing >integers followed by monotonically decreasing integers. For example, >[1,2,5,8,11,6,3,-1] is a hill, but [1,2,5,8,11,6,9,3,-1] and [1,2,3,4,5,6] are >not hills. You may assume that IntList contains only integers.

这是我到目前为止所做的:

hill(List) :-
increasing(List), decreasing(List).

increasing([H|Tail]) :-
sm(H,Tail),
increasing(Tail).
increasing([]).


decreasing([H|Tail]) :-
gr(H,Tail),
decreasing(Tail).

decreasing([]).

hill([]).

gr(X,[H|Tail]) :- X>H.
gr(X,[]).

sm(X,[H|Tail]) :- X<H.
sm(X,[]).

但这行不通。逻辑是:数字列表是 hill 如果它是 increasing 然后 decreasing。我怎么说呢?此代码执行 increasingdecreasing,但没有列表可以既 increasingdecreasing

有什么想法吗?

最佳答案

我不想为家庭作业问题提供完整、有效的解决方案,但我会用文字描述我将如何从您现在获得的代码开始。现在你的 increasingdecreasing谓词测试整个列表。但是,根据您的定义,一座山既不完全增加也不完全减少。我会将这些谓词修改为具有两个参数而不是一个。附加参数将绑定(bind)到不满足增加/减少标准的列表的尾部。然后,我会稍微修改 hill 以使用 increasing 的新参数测试不是整个列表的递减,而是初始递增子序列之后的部分的递减。最后,我会使用 decreasing 的新参数验证递减子序列后没有非递减元素。

如果您需要更好的提示,或者如果我似乎在胡说八道(很有可能,因为我对 Prolog 不是那么好),请告诉我,我会尽力澄清更多。

根据 OP 的评论进行编辑: 好的,让我们尝试其他方法。 L是一座山当且仅当L是至少两个以某个元素结尾的单调递增元素的列表 M ,后跟至少一个以某个元素开头的单调递减元素的列表 N , 其中N < M .你能把那个描述翻译成 Prolog 子句吗?

编辑两段(剧透警告):


在您修改后的代码中,删除这三个谓词:increasing([]). , hill([]). , 和 hill(List) :- decreasing(List). .这几乎会给你一个解决方案,但它仍然会失败,例如在 [3, 2, 1] .不过,解决这个问题应该相当容易。

关于list - Prolog IntList 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1736589/

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