What's the explanation for Exercise 1.6 in SICP?

百般思念 提交于 2019-12-20 08:31:24

问题


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:

(define (new-if predicate then-clause else-clause)
    (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)
            guess
            (sqrt-iter (improve guess x)
                       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.


回答1:


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.




回答2:


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. :)




回答3:


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>)

To evaluate an if expression, the interpreter starts by evaluating the <predicate> part of the expression. If the <predicate> evaluates to a true value, the interpreter then evaluates the <consequent> and returns its value. Otherwise it evaluates the <alternative> and returns its value.




回答4:


Ex1.6. new-if:

(define (new-if predicate then-clause else-clause)
     (cond (predicate then-clause)
                (else else-clause)))

Difference with ‘if-statements’: if-statements evaluate one by one from predicate -> consequent -> alternative,

however the ‘new-if’ has to evaluate all parameters aka arguments the MOMENT its called(which means 'else-clause' is evaluated at the start!!),

and thus this causes an infinite loop when any of these parameters call themselves into an iterative loop



来源:https://stackoverflow.com/questions/1171252/whats-the-explanation-for-exercise-1-6-in-sicp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!