Recursive functions are more general than looping!
-
Instead of going forward, we can also go backward.
-
We can look at the list from the back to the front: start at the end of the list.
What's the difference?
With forward recursion:
-
We do the work for the current element
first.
-
Then we do the recursive call last to process the rest of the elements in the data structure.
With backward recursion:
-
We do the recursive call to process the
rest of the elements in the data structure first.
Then we combine the result of the recursive call with the work for the current element last.
-
I don't need a result parameter in my helper function!