5.11. Leap of faith¶
Following the flow of execution is one way to read programs, but as you saw in the previous section, it can quickly become labarynthine. An alternative is what I call the “leap of faith.”
Note
When you come to a function call, instead of following the flow of execution, you assume that the function works correctly and returns the appropriate value. This is the leap of faith.
In fact, you are already practicing this leap of faith when you use built-in functions. When you call ‘’cos’’ or ‘’exp’’, you don’t examine the implementations of those functions. You just assume that they work, because the people who wrote the built-in libraries were good programmers.
Well, the same is true when you call one of your own functions. For example, in Section 5.8 we wrote a function called ‘’isSingleDigit’’ that determines whether a number is between 0 and 9. Once we have convinced ourselves that this function is correct—by testing and examination of the code—we can use the function without ever looking at the code again.
The same is true of recursive programs. When you get to the recursive call, instead of following the flow of execution, you should assume that the recursive call works (yields the correct result), and then ask yourself, “Assuming that I can find the factorial of \(n-1\), can I compute the factorial of \(n\)?” In this case, it is clear that you can, by multiplying by \(n\).
Of course, it is a bit strange to assume that the function works correctly when you have not even finished writing it, but that’s why it’s called a leap of faith!
- Since you can find the factorial of the first 3 numbers in a list, you must also be able to find the factorial of the first 2.
- There is no leap of faith here. In fact, it's quite flip-flopped.
- You assume that the log function works, without examining the implementation.
- This is a leap of faith, but it is not recursive.
- Since you can take the sum of the first 7 numbers in a list, you assume that you can also take the sum of the first 8.
- Recursive leaps of faith always assume that the recursive call will work.
- You write a function and assume that it will work.
- This is a leap of faith, but it is not recursive.
Q-1: Which of the following is an example of the recursive leap of faith?