输出似乎只测试列表中的最后一项是否有差异函数 [英] Output seems to only test the very last in the list for difference function
问题描述
链接到旧问题:检查是否存在差异连续元素之间是相同的
我在另一个问题中发布了我的进度,但是这是我的代码,这是我试图在Prolog中解决的问题.我希望函数根据每个数字之间的差异是否与我的最后一个参数相同来返回sameSeqDiffs([3,5,7],2)的结果.到目前为止,这是我要介绍的内容:
I have posted my progress in another question post, but here is my code my the problem I am trying to solve in Prolog. I would like my function to return the result of sameSeqDiffs([3,5,7],2) depending on whether the difference between each number is the same as my last argument. Here is what I cam up with so far:
sameSeqDiffs([X,Y], Result):-
A is Y - X,
A = Result.
sameSeqDiffs([X,Y,Z|T], Result):-
sameSeqDiffs([Y,Z|T], Result).
当我测试此代码时,它似乎可以用于某些输入,但显然不能用于其他输入:
When I test this code, it seems to work for some input, but clearly fails for others:
推荐答案
您的解决方案存在一些问题:
Your solution has some issues:
sameSeqDiffs([X,Y,Z|T], Result):-
sameSeqDiffs([Y,Z|T], Result).
在这里,您将完全忽略变量X和差异X-Y.
Here you ignore completely variable X and the difference X-Y.
sameSeqDiffs([X,Y], Result):-
Result is Y - X.
sameSeqDiffs([X,Y,Z|T], Result):-
Result is Y - X,
sameSeqDiffs([Y,Z|T], Result).
这篇关于输出似乎只测试列表中的最后一项是否有差异函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!