Take the 2-minute tour ×

I'm just beginning to work through SICP (on my own; this isn't for a class), and I've been struggling with Exercise 1.6 for a couple of days and I just can't seem to figure it out. This is the one where Alyssa re-defines if in terms of cond, like so:

    (cond (predicate then-clause)
          (else else-clause))

She tests it successfully on some simple cases, and then uses it to re-write the square root program (which worked just fine with if):

(define (sqrt-iter guess x)
    (new-if (good-enough? guess x)
            (sqrt-iter (improve guess x)

The question then asks: "What happens when Alyssa attempts to use this to compute square roots? Explain." [If necessary, I'm happy to reproduce the 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 recurses infinitely. I just can't explain why this happens. Whatever subtle difference exists between if and new-if is eluding me. Any and all help much appreciated.

share|improve this question
The verb form of "recursive" is "to recurse", so it "recurses". –  Greg Hewgill Jul 23 '09 at 11:57
your question title is wrong: you are referring to Exercise 1.6, not 1.4. –  systemovich Dec 12 '10 at 15:24
@Geoffrey Van Wyk You're right. At the time I wrote the question, I was working through my old copy of the 1st Edition of SICP, in which this problem appears as Exercise 1.4. In the 2nd Edition, it's Exercise 1.6. I'll make the change. –  Alex Basson Dec 12 '10 at 16:36
add comment

3 Answers

up vote 24 down vote accepted

new-if is a function. When a function is called, what's the first thing that Scheme does with the argument list? It evaluates all the arguments.

share|improve this answer
add comment

First of all you have to understand the difference between applicative order evaluation and normal order. Lisp uses applicative order, but conditional expressions are evaluated not like normal functions (sicp chapter 1.1.6):

(if <predicate> <consequent> <alternative>)


share|improve this answer
add comment

new-if is a procedure, and Scheme uses applicative-order evaluation (1.1.5), so even before new-if is actually performed, it has to evaluate all the arguments first, which are guess and (sqrt-iter (improve guess x) x). You can see that the latter argument is a recursion, which calls a new new-if procedure, this is how the infinite loop occurs.

The ordinary if need not evaluate its arguments first, just go along the way, this is the difference between if and new-if. :)

share|improve this answer
add comment

Your Answer


