Stop learning alone!

Learn faster and stay on-track by joining this free class with other self-learners.

Register for Structure and Interpretation of Computer Programs now.

Structure and Interpretation of Computer Programs

Class length: 13 weeks. Start anytime.

Creator: kday

Status: Established

Join this class!

Exercise 1.6 question

I'm not sure why the new-if procedure using the cond expression does not work and enters an infinite loop. If this is due to applicative order, as someone's homework provides as the answer, then why doesn't applicative order apply to the special form 'if'?

reznite
2 years ago

Reply


I was struggling with this one, too. After seeing the other answers in the class so far, and a quick google search, and some more digging through the book, I finally understood what happens and why.

Section 1.1.6 of the book explains how 'cond' and 'if' work. They are "special forms" in that they do not evaluate in applicative order. They evaluate the first predicate only, and what it does from that point on depends on what that first predicate returns. 'new-if' is a procedure, in which applicative order evaluates all it's operands, including 'sqrt-iter' again, which has 'new-if' that calls 'sqrt-iter'...which causes an infinite loop.

hyp3rVigi1ant
2 years ago

Reply

I thought the problem originates from the difference (if any) of the 'cond' and 'if' operators. I tried new-if implementation with 'if' also, and problem continues.

Thanks for the clarification. It is clear now, which is not that clear from the answers.

spx
2 years ago

Reply

* Markdown Cheatsheet:

Link:
[clickable text](http://www.example.com)

New Paragraph:
Hit enter twice

Main heading:
# Main Heading Text

Sub-heading:
## Sub-heading Text

List:
* item 1
* item 2
* item 3

Italics:
*italicized text*

Bold:
**bold text**

YouTube:
URL (http://www.youtube.com/watch?v=Ui4AYPcRkYE) turns into embed code

Full Markdown reference