-1

我正在尝试理解 Scala 模式匹配函数,但我似乎无法理解这里发生的事情。

sealed abstract class SearchTree
  case object Empty extends SearchTree
  case class Node(l: SearchTree, d: Int, r: SearchTree) extends SearchTree

  def test(t: SearchTree): Boolean = {
    def check(t: SearchTree, min: Int, max: Int): Boolean = t match {
      case Empty => true
      case Node(l, d, r) =>  min <= d && d < max && check(l, min, d) && check(r, d, max)
    check(t, Int.MinValue, Int.MaxValue)
  }

我的问题是:初始匹配调用传递的值 min: Int, max: Int 在哪里?在那之后,我理解了连续的递归调用,但是我们如何才能开始模式匹配,只使用 t:SearchTree 作为已知值。

我确实对此功能进行了测试,但我不认为它们对于该问题是必要的

提前致谢

4

1 回答 1

2

minmax通过调用checkattest与 this call传递给函数check(t, Int.MinValue, Int.MaxValue)。所以test所做的就是checkt,Int.MinValue和调用Int.maxValuemin并且max没有通过初始match调用传递它们在范围内,因此可以在 case 语句中访问。

于 2013-10-09T05:37:27.453 回答