为什么此Scala代码未选中

时间:2016-02-23 02:52:40

标签: scala types functional-programming type-systems

我想编写一个函数来计算二叉树的大小,函数'size'工作得很好,但它太慢了。然后我写了一个名为'size2'的函数,但是,它没有进行类型检查。为什么呢?

sealed trait Tree[+A]
case class Leaf[A](value: A) extends Tree[A]
case class Branch[A](left: Tree[A], right: Tree[A]) extends Tree[A]

def size[A](tree: Tree[A]): Int = tree match {
  case Leaf(_) => 1
  case Branch(l, r) => size(l) + size(r) + 1
}

def size2[A](tree: Tree[A]): Int = {
  trait Timing
  case object First extends Timing
  case object TravelLeft extends Timing
  case object TravelRight extends Timing
  type Stack = List[(Tree[A], Timing)]

  def count(history: Stack, res: Int): Int = history match {
    case Nil => res
    case (Leaf(_), _)::tail => count(tail, res + 1)
    case (b@Branch(l, _), First)::tail =>
      val next = (l, First)::(b, TravelLeft)::tail
      count(next, res)
    case (b@Branch(_, r), TravelLeft)::tail =>
      val next = (r, First)::(b, TravelRight)::tail
      count(next, res)
    case (_, TravelRight)::tail => count(tail, res + 1)
  }
  count(List((tree, First)), 0)
}

错误消息如下:

Error:(19, 23) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(A$A301.this.Tree[A], Timing)]
    case (Leaf(_), _)::tail => count(tail, res + 1)
                     ^
Error:(21, 34) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(A$A301.this.Tree[A], Timing)]
    case (b@Branch(l, _), First)::tail =>
                                ^
Error:(24, 39) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(A$A301.this.Tree[A], Timing)]
    case (b@Branch(_, r), TravelLeft)::tail =>
                                     ^
Error:(27, 27) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(A$A301.this.Tree[A], Timing)]
    case (_, TravelRight)::tail => count(tail, res + 1)
                         ^
Error:(66, 23) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(inst$A$A.Tree[A], Timing)]
    case (Leaf(_), _)::tail => count(tail, res + 1)
                     ^
Error:(68, 34) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(inst$A$A.Tree[A], Timing)]
    case (b@Branch(l, _), First)::tail =>
                                ^
Error:(71, 39) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(inst$A$A.Tree[A], Timing)]
    case (b@Branch(_, r), TravelLeft)::tail =>
                                     ^
Error:(74, 27) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(inst$A$A.Tree[A], Timing)]
    case (_, TravelRight)::tail => count(tail, res + 1)
                         ^

1 个答案:

答案 0 :(得分:0)

=======================更新======================= ====

感谢som-snytt的帮助。引发此错误是因为我在同一个包中定义了一个List。查看错误消息,它想要一个test.List对象,而:: object创建一个标准库List,它不会被选中

=============================================== =========

好的,谢谢Wickoo的评论。我在Scala文件中再次尝试过,它运行正常。此错误仅在scala工作表中引发,我不知道为什么。