我正在尝试将此haskell max函数实现移植到scala
maximum' :: (Ord a) => [a] -> a
maximum' [] = error "maximum of empty list"
maximum' [x] = x
maximum' (x:xs) = max x (maximum' xs)
这是我的第一次尝试:
def max[T <: Ordered[T]](list: List[T]): T = list match {
case Nil => throw new Error("maximum of empty list")
case head :: Nil => head
case list => {
val maxTail = max(list.tail)
if (list.head > maxTail) list.head else maxTail
}
}
max(List[Int](3,4))
但我收到以下错误:
inferred type arguments [Int] do not conform to method max's type parameter bounds [T <: Ordered[T]]
我尝试了类似结果的订购,可比等......
关于缺少什么的任何想法?