Exercise 1.22 of SICP contains the following procedure:
(define (start-prime-test n start-time)
(if (prime? n)
(report-prime (- (runtime) start-time))))
To my eyes, the if
form has no alternative branch. I can only see if (test) (consequent)
. Where does SICP introduce this form? if
is introduced in Section 1.1.6 but I see nowhere in that section that covers "if
without else
" cases. It is possible that I could guess the intended behavior from the definition of cond
in that same section, but said section has a footnote that shows that the two are different. So how can I tell how SICP intends such if
forms to work?
After consulting the index, I discovered two places where it is defined: Footnote 29 in Section 3.3 and Footnote 10 in Section 4.1. Quoting both:
Observe that the if expression in this procedure has no < alternative > expression. Such a ``one-armed if statement'' is used to decide whether to do something, rather than to select between two expressions. An if expression returns an unspecified value if the predicate is false and there is no < alternative >.
The value of an if expression when the predicate is false and there is no alternative is unspecified in Scheme; we have chosen here [in The Metacircular Evaluator] to make it false. We will support the use of the variables true and false in expressions to be evaluated by binding them in the global environment. See section 4.1.4.
I admit that I should've thought to consult the Index before consulting Stack Overflow. Clearly, the internet has rotten my brain. However, I hope that there is a better answer to this question than the one that I've given here. "It's defined two chapters after you need it", even if correct, isn't very pleasant.
On the bright side, codybartfast's comments have rightly pointed out that you don't need to understand the procedure described in Exercise 1.22 to solve the question. In fact, having solved it myself, it's clear that I was immensely overthinking it. It can be solved in less than five lines. Furthermore, it has also been pointed out that, although its first few words happen to match the standard for Scheme, the second quote above is largely talking about the implementation that is being built in chapter 4, rather than anything that's expected to be used or understood in the earlier chapters.