I made recursive function, just like
require : L (List[Int])
L pattern matching
Nil => Thread.dumpStack()
x :: xs => print(x) + function(xs)
def function(L : List[Int]) { L match { case Nil => Thread.dumpStack() case x :: xs => print(x + " "); function(xs) } }
val l = (1 to 5).toList // function(l)
So I think this function in the stack frame n times, but it occurs one time, I think this function has already found Nil
and print out exception Thread.dumpStack
.
Is scala compiler smart or other else?
You are observing tail recursion: there is nothing to store from one iteration to the next, so the recursion is essentially turned into a while loop by the compiler. (So, yes, the compiler is smart in that way.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With