I am trying to implement a memoized Fibonacci number function, and I am running into a compile error that I can't sort out. The following code is what I have so far.
var fibs = Map.empty[Int, Int]
fibs += 0 -> 1
fibs += 1 -> 1
fibs += 2 -> 2
val fib = (n: Int) => {
if (fibs.contains(n)) return fibs.apply(n)
else{
// Error here
val result = fib(n - 1) + fib(n - 2)
fibs+= n -> result
return result
}
}
println(fib(100))
The error is:
Recursive
fibneeds type
I have tried entering a return type for the closure in various places, but I can't seem to get it to work.
Declaring the closure like val fib = (n: Int): Int => { yields a different compile error.
Could you please help me fix this compile error?