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 ProgramsClass length: 13 weeks. Start anytime. Creator: kday Status: Established |
Join this class! |
|
Exercise 1.6 questionI'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'? 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. 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.
* Markdown Cheatsheet:
Link:
New Paragraph:
Main heading:
Sub-heading:
List:
Italics:
Bold:
YouTube:
|