这是我的文件
trait Set[T] {
def contains(x: T): Boolean
def incl(x: T): Set[T]
def union(that: Set[T]): Set[T]
}
class Empty[T] extends Set[T] {
override def toString = "."
def contains(x: T): Boolean = false
def incl(x: T): Set[T] = new NonEmpty[T](x, new Empty[T], new Empty[T])
def union(that: Set[T]): Set[T] = that
}
class NonEmpty[T](elem: T, left: Set[T], right: Set[T]) extends Set[T] {
override def toString = "{" + left + elem + right + "}"
def contains(x: T): Boolean =
if (x < elem) left contains x
else if (x > elem) right contains x
else true
def incl(x: T): Set[T] =
if (x < elem) new NonEmpty(elem, left incl x, right)
else if (x > elem) new NonEmpty(elem, left, right incl x)
else this
def union(that: Set[T]): Set[T] =
((left union right) union that) incl elem
}
我使用了“:贴”的方法,因为:负载不工作。 但我得到以下错误
<console>:25: error: value < is not a member of type parameter T
if (x < elem) left contains x
^
<console>:26: error: value > is not a member of type parameter T
else if (x > elem) right contains x
^
<console>:30: error: value < is not a member of type parameter T
if (x < elem) new NonEmpty(elem, left incl x, right)
^
<console>:31: error: value > is not a member of type parameter T
else if (x > elem) new NonEmpty(elem, left, right incl x)
我敢肯定,这个文件是正确的,因为它是从类的例子,当教授利用在课堂上它的工作...
任何帮助?