What am I missing here?
This is the error I am getting…
error: type mismatch; found : List[Double](in method
calculateHaarWaveletI)]required: List[scala.Double]
Console.println(list2Tuples(ls.take(n)))
Here is my code..
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....")
}
}
Your type parameter
Doublein the definition ofcalculateHaarWaveletIis shadowingscala.Double. You can just remove the type parameter altogether and the code should work as expected. See my answer here for more details.