An Inductive Proof of the Correctness of foo
- Base Case: foo squares argument 0, because when given 0
foo returns 0, and the square of 0 is 0.
- Induction Hypothesis (IH): Assume that foo squares
n-1.
- Inductive Step:
- (foo n) returns foo applied to n-1 plus 2n-1 (by
inspection of foo and elementary algebra)
- foo applied to n-1 is (n-1)2 (by
the IH)
- (n-1)2 = n2 - 2n + 1 (by elementary algebra)
- (foo n) returns n2 - 2n + 1 + 2n - 1 (by steps
1, 2, and 3)