<< Previous exercise (1.5) | sicp-solutions | Next exercise (1.7) >>
The default if statement is a special form which means that even when an interpreter follows applicative substitution, it only evaluates one of its parameters- not both. However, the newly created new-if doesn't have this property and hence, it never stops calling itself due to the third parameter passed to it in sqrt-iter.
To be even clearer: The act of re-defining a special form using generic arguments effectively "De-Special Forms" it. It then becomes subject to applicative-order evaluation, such that any expressions within the consequent or alternate portions are evaluated regardless of the predicate. In Ex 1.6, the iteration procedure is called without return and eventually overflows the stack causing an out of memory error.
(define (iff <p> <c> <a>) (if <p> <c> <a>)) (define (tryif a) (if (= a 0) 1 (/ 1 0))) (define (tryiff a) (iff (= a 0) 1 (/ 1 0)))
Welcome to DrRacket, version 7.5 [3m]. Language: R5RS; memory limit: 128 MB. > (tryif 0) 1 > (tryif 1) . . /: division by zero > (tryiff 0) . . /: division by zero > (tryiff 1) . . /: division by zero >
(Note: comments below apply to a previous version of this solution, which has been changed to take them into account; please refer to revisions 1–12 of the edit history to view the version on which they were made.)