检查数组[A]是否被排序? [英] Checking whether an Array[A] is sorted?

查看:198
本文介绍了检查数组[A]是否被排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


执行isSorted,它检查数组[A]中是否存在数组[按照给定的比较函数进行排序:



  def isSorted [A](as:Array [A],命令:(A,A)=>布尔值):Boolean 

回答以下作者提供的解决方案:

  //练习2:实现一个多态函数来检查
// an`Array [A]`sort
def isSorted [A](as:Array [A],gt:(A,A)=> Boolean):Boolean = {$ b $如果(n> = as.length-1)true
else if(gt(as(n), as(n + 1)))false
else go(n + 1)

go(0)
}
pre>

我被以下代码行弄糊涂了: else if(gt(as(n),as(n + 1)) )false



我认为在Scala中使用函数作为参数时,需要实际定义一个在当前函数范围之外的独立函数(即,有另外一个函数定义了 gt 需要做什么)?我没有看到在其他地方定义过的 gt ,那么它如何能够提供一个布尔值 code> isSorted ?



我的假设是正确的,还是我完全缺少使用函数作为参数的东西?在这种情况下, gt

是一个传递给 isSorted 作为参数的函数;
让我们将它与任何其他参数进行比较,比如 a:Int 。让我们看看下面的函数:

  def increaseByOne(a:Int):Int = a + 1 

code>

您对 gt 的问题等同于询问: How如果 a 永远不会被分配一个值,那么 increaseByOne 使用 a 。显然, increaseByOne 方法的 caller 必须传递一些值作为 a ,但是参数的概念是命名一些提供给该方法的值。



对于 gt :它是 isSorted 的一个参数。在你提供的代码中,我们没有看到 isSorted 调用者,所以我们没有看到 gt 可能 - 但显然是 isSorted 实现不需要假设任何关于 gt 被创建,它作为参数传递,因此可以用在 isSorted 中。



我们可以想象一个用法,例如:

$ $ $ $ $ $ $ $ $ isSorted(Array(1,3,6),( a:Int,b:Int)=> a> b)

$ c>(a:Int,b:Int)=> a> b 应该是 gt 的值。

While working through a problem found in 'Functional Programming in Scala':

Implement isSorted, which checks whether an Array[A] is sorted according to a given comparison function:

def isSorted[A](as: Array[A], ordered: (A,A) => Boolean): Boolean

While comparing my answer to the below solution provided by the author(s):

  // Exercise 2: Implement a polymorphic function to check whether
  // an `Array[A]` is sorted
  def isSorted[A](as: Array[A], gt: (A,A) => Boolean): Boolean = {
    @annotation.tailrec
    def go(n: Int): Boolean =
      if (n >= as.length-1) true
      else if (gt(as(n), as(n+1))) false
      else go(n+1)

    go(0)
  }

I am getting confused by the following line of code: else if (gt(as(n), as(n+1))) false

I thought when using a function as a parameter in Scala, one would need to actually define a separate function outside the scope of the current function (i.e. have another function defined that does what gt would need to do)? I don't see gt defined anywhere else, so how is it able to provide a Boolean value to use in isSorted?

Are my assumptions correct, or am I completely something missing here with using functions as parameters? A detailed explanation is much appreciated.

解决方案

In this case, gt is a function passed to isSorted as an argument; Let's compare it to any other argument, say a: Int. Let's look at the following function:

def increaseByOne(a: Int): Int = a + 1

Your question about gt is equivalent to asking: "How can increaseByOne use a if a is never assigned a value?". Obviously, the caller of the increaseByOne method would have to pass some value as the value of a, but the very concept of arguments is naming some value that is provided into the method.

The same goes for gt: it's an argument of isSorted. In the code you presented, we don't see the caller of isSorted, so we don't see any example of what gt might be - but obviously the implementation of isSorted need not assume anything about how and where gt is created, it is passed as an argument and therefore can be used in isSorted.

We can imagine a usage such as:

isSorted(Array(1,3,6), (a: Int, b: Int) => a > b)

Here, (a: Int, b: Int) => a > b would be the value of gt.

这篇关于检查数组[A]是否被排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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