gpt4 book ai didi

map 上的 Scala 类型不匹配错误

转载 作者:行者123 更新时间:2023-12-02 22:38:50 25 4
gpt4 key购买 nike

我在这里错过了什么?这是我收到的错误...

error: type mismatch; found : List[Double](in method calculateHaarWaveletI)]

required: List[scala.Double]

Console.println(list2Tuples(ls.take(n)))

这是我的代码..

    object HaarWavelet {

def calculateHaarWavelet(ls: List[Double]): List[Double] = {
if (ls.length % 2 != 0) throw new RuntimeException("Need even number of elements to calculate HaarWavelet")
calculateHaarWaveletI(ls, ls.length)
def calculateHaarWaveletI[Double](ls: List[Double], n: Int): List[Double] = {
Console.println(list2Tuples(ls.take(n)))
null
}
null
}

def processTuple(x: (Double, Double)): (Double, Double) = {
val f = (x._1 + x._2) / 2
(f, x._1 - f)
}

def list2Tuples(ls: List[Double]): List[(Double, Double)] = {
if (ls.isEmpty) return List()
(ls.head, ls.tail) match {
case (_, Nil) => List()
case (x, y) => List((x, y.head)) ::: list2Tuples(y.tail)
}
}

def main(args: Array[String]) {
Console.println("Starting....")
Console.println(calculateHaarWavelet(List(8.0, 4.0)))
Console.println("Done....")
}
}

最佳答案

calculateHaarWaveletI 定义中的类型参数 Double 正在隐藏 scala.Double。您可以完全删除类型参数,代码应该按预期工作。看我的回答here了解更多详情。

关于 map 上的 Scala 类型不匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11037870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com