When trying to run a polymorphic function that abstracts over the type of the array, I get a type mismatch with the following error message:
Type mismatch: expected: (Int) => Boolean, actual: Int
val listIntegers = List(55, 50, 25, 23, 21, 16, 15)
def abstractSearchKey[A](a: List[A], p: A => Boolean): Int = {
def loop(n: Int): Int ={
if (n > a.length) -1
else if (p(a(n))) n
else loop(n+1)
}
loop(0)
}
abstractSearchKey[Int](listIntegers, 25)
I'm confused about this error message because this exercise is based on Chiusano's Functional Programming with Scala. In fact, on page 24, the authors state:
...the p function must accept a value of type A (since it's a function of type A => Boolean)
Any advice or feedback on this matter would be greatly appreciated!