I am just starting to work through SICP (by itself, this is not for the class), and I struggled with exercise 1.6 for a couple of days, and I just canβt understand what it is outside. This is where Alyssa redefines if in terms of cond , for example:
(define (new-if predicate then-clause else-clause) (cond (predicate then-clause) (else else-clause))
She successfully tests it in some simple cases, and then uses it to re-write a program with a square root (which works fine with if ):
(define (sqrt-iter guess x) (new-if (good-enough? guess x) guess (sqrt-iter (improve guess x) x)))
Then the question arises: "What happens when Alyssa tries to use this to calculate the square roots? Explain." [If necessary, I am happy to reproduce other procedures ( good-enough? improve , etc.), just let me know.]
Now I know what happens: it never returns a value, which means that the program is infinitely recursive. I just can't explain why this is happening. Whatever the subtle difference between if and new-if , eludes me. Any help is greatly appreciated.
recursion sicp
Alex Basson Jul 23 '09 at 11:53 2009-07-23 11:53
source share