I’ve been always having trouble to understand recursion. I know how to write some simple ones though.
Take this sum calculation Scala program for example. Please don’t answer this question based on the Scala specific implementation. I’d like to have a generic understanding on recursion.
1. It is calling sum(n-1) inside sum(n). Are they (sum(n) and sum(n-1)) using different instance of the same function?
2. How can the compiler understand this function when it is calling itself (sort of unfinished based on my view)?
3. What is the normal procedure of writing recursive functions (design recursive algorithms)?