【发布时间】:2016-06-04 17:19:24
【问题描述】:
我想写一个函数来计算二叉树的大小,函数'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)
^
【问题讨论】:
-
你的 scala 版本是什么?看起来像是为我编译的。
-
我的scala版本是2.11.7
-
我用的是同一个版本。我只收到以下警告:“它会在以下输入上失败:List(_) def count(history: Stack, res: Int): Int = history match { ^ one warning found”我复制粘贴了您的代码并将您的对象内的两个大小方法。
-
我找到了原因,因为我在scala工作表中编码。但为什么它在 scala 工作表中不起作用?这是一个错误吗?
-
您使用的是 ScalaIDE (Eclipse) 还是 IntelliJ Scala 插件?我认为这两种工具都有自己的编译器前端(解析器/类型检查器等),并且它们推断类型的方式可能存在错误。然而,如果 scalac 编译它,这意味着你的代码是正确的。
标签: scala types functional-programming type-systems