检查连续元素之间的差异是否相同 [英] Checking if the difference between consecutive elements is the same

查看:50
本文介绍了检查连续元素之间的差异是否相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Prolog中使用算术的新手.

I am new to using arithmetic in Prolog.

我做了一些小程序,但主要涉及逻辑.我正在尝试实现一个函数,如果每对连续的元素对之间的差异相同或不同,则该函数将返回truefalse.

I’ve done a few small programs, but mostly involving logic. I am trying to implement a function that will return true or false if the difference between every consecutive pair of elements is the same or not.

我的输入应如下所示:sameSeqDiffs([3, 5, 7, 9], 2) 我觉得我需要从列表中拆分前两个元素,找到它们的区别,然后将结果添加到新列表中.处理完所有元素后,检查新列表中的元素是否全部相同.

My input would look like this: sameSeqDiffs([3, 5, 7, 9], 2) I feel like I need to split the first two elements from the list, find their difference, and add the result to a new list. Once all the elements have been processed, check if the elements of the new list are all the same.

我已经教过一些Prolog,其中包括建立关系并进行查询,但这似乎不适用于Prolog.

I’ve been taught some Prolog with building relationships and querying those, but this doesn’t seem to fit in with Prolog.

Update1:​​这是我到目前为止提出的.我是这种语法的新手,但是我的代码仍然出现错误,但是我希望它能传达我正在尝试做的一般想法.

Update1: This is what I've come up with so far. I am brand new to this syntax and am still getting an error on my code, but I hope it conveys the general idea of what I'm trying to do.

diff([X,Y|Rest], Result):-
    diff([Y,Z|Rest], Result2):-
       Result2 = Result,
       Z - Y = Result.

Update2:我知道我仍然需要对这段代码做很多事情,但是这是我将一直待到本周末的地方,我还有其他事情要做.我想我对它的逻辑有了更多的了解,并且我认为只有在列表的其余部分中至少还有两件事要处理时,我才需要弄清楚如何运行函数的最后一行.

Update2: I know I still have much to do on this code, but here is where I will remain until this weekend, I have some other stuff to do. I think I understand the logic of it a bit more, and I think I need to figure out how to run the last line of the function only if there is at least two more things in the rest of the list to process.

diff([X,Y|Rest], Result):-
    number(Y),
    Y-X=Result,
    diff([Rest], Result).

Update3:我相信我具有我想要的功能.我注意到的唯一古怪之处是,当我运行并输入以下内容时:sameSeqDiffs([3,5,7],2).我立即返回true,然后返回false.这是正确的操作还是我仍然缺少某些东西?

Update3: I believe I have the function the way I want it to. The only quirk I noticed is that when I run and input like: sameSeqDiffs([3,5,7],2).I get true returned immediately followed by a false. Is this the correct operation or am I still missing something?

sameSeqDiffs([X,Y], Result):-
    A is Y - X,
    A = Result.

sameSeqDiffs([X,Y,Z|T], Result):-
    sameSeqDiffs([Y,Z|T], Result).

更新4:我对此发布了一个新问题....这里是链接:

Update 4: I posted a new question about this....here is the link: Output seems to only test the very last in the list for difference function

推荐答案

Prolog的语法

语法略有偏离:通常,子句的头部类似于foo(X, Y, Z),然后是箭头(:-),后跟主体.该主体通常不包含任何箭头:-.因此第二个箭头:-没有太大意义.

Prolog's syntax

The syntax is a bit off: normally a clause has a head like foo(X, Y, Z), then an arrow (:-), followed by a body. That body normally does not contain any arrows :-. So the second arrow :- makes not much sense.

第二个在Prolog谓词中没有 input output ,谓词是truefalse(当然,它也可能出错,或者陷入无限大的状态)循环,但这通常是我们要避免的行为).它通过 unifying 变量传达答案.例如,呼叫sameSeqDiffs([3, 5, 7, 9], X).可以通过将X2统一来成功,然后谓词(如果正确实现的话)将返回true..

Secondly in Prolog predicates have no input or output, a predicate is true or false (well it can also error, or got stuck into an infinite loop, but that is typically behavior we want to avoid). It communicates answers by unifying variables. For example a call sameSeqDiffs([3, 5, 7, 9], X). can succeed by unifying X with 2, and then the predicate - given it is implemented correctly - will return true..

为了设计谓词,通常首先要想出一个归纳定义:由一个或多个基本案例以及一个或多个递归"案例组成的定义(谓词是由其自身的部分定义的.)

In order to design a predicate, on typically first aims to come up with an inductive definition: a definition that consists out of one or more base cases, and one or more "recursive" cases (where the predicate is defined by parts of itself).

例如,在这里我们可以说:

For example here we can say:

(基本情况)对于正好两个元素[X, Y]的列表,只要DYX之差,则谓词sameSeqDiffs([X, Y], D)成立. >

(base case) For a list of exactly two elements [X, Y], the predicate sameSeqDiffs([X, Y], D) holds, given D is the difference between Y and X.

在Prolog中,它看起来像:

In Prolog this will look like:

sameSeqDiffs([X, Y], D) :-
    ___.

(必须填写___).

现在,对于归纳情况,我们可以就其本身来定义sameSeqDiffs/2,尽管 not 当然具有相同的参数.在数学中,有时会定义一个函数 f ,例如 f(i)= 2× f(i-1);以例如 f(0)= 1 为基础.我们可以用类似的方式为sameSeqDiffs/2定义一个归纳案例:

Now for the inductive case we can define a sameSeqDiffs/2 in terms of itself, although not with the same parameters of course. In mathematics, one sometimes defines a function f such that for example f(i) = 2×f(i-1); with for example f(0) = 1 as base. We can in a similar way define an inductive case for sameSeqDiffs/2:

(归纳大小写)对于包含两个以上元素的列表,给定前两个元素具有差异D,且列表中的所有元素均具有相同的差异.除第一个元素外的所有元素,所有元素也具有D差异.

(inductive case) For a list of more than two elements, all elements in the list have the same difference, given the first two elements have a difference D, and in the list of elements except the first element, all elements have that difference D as well.

在Prolog中,它看起来像:

In Prolog this will look like:

sameSeqDiffs([X, Y, Z|T], D) :-
    ___,
    sameSeqDiffs(___, ___).

Prolog中的算法

开始使用Prolog编程的人经常犯的一个错误是,他们认为,就像许多编程语言中常见的那样,Prolog向某些函子添加了语义.

Arithmetic in Prolog

A common mistake people who start programming in Prolog make is they think that, like it is common in many programming languages, Prolog add semantics to certain functors.

例如,人们可以认为A - 1将减少A.对于Prolog,这只是-(A, 1),不是减号,或其他任何东西,只是一个函子.结果,Prolog将评估这些表达式.因此,如果您写X = A - 1,则X就是X = -(A,1).

For example one can think that A - 1 will decrement A. For Prolog this is however just -(A, 1), it is not minus, or anything else, just a functor. As a result Prolog will not evaluate such expressions. So if you write X = A - 1, then X is just X = -(A,1).

那么我们如何执行数字运算? Prolog系统具有谓词is/2,该谓词通过将语义附加到右手侧来评估右手侧.因此,is/2谓词将解释(+)/2(-)/2等函子((+)/2为正号,(-)/2为负号等).

Then how can we perform numerical operations? Prolog systems have a predicate is/2, that evaluates the right hand side by attaching semantics to the right hand side. So the is/2 predicate will interpret this (+)/2, (-)/2, etc. functors ((+)/2 as plus, (-)/2 as minus, etc.).

因此我们可以评估一个表达式,例如:

So we can evaluate an expression like:

A = 4, is(X, A - 1).

,然后将X设置为3,而不是 4-1. Prolog还允许编写is中缀,例如:

and then X will be set to 3, not 4-1. Prolog also allows to write the is infix, like:

A = 4, X is A - 1.

在这里您将需要使用它来计算两个元素之间的差异.

Here you will need this to calculate the difference between two elements.

这篇关于检查连续元素之间的差异是否相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆