What's the explanation for Exercise 1.6 in SICP?

前端 未结 6 505
一个人的身影
一个人的身影 2021-01-30 16:08

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

6条回答
  •  北海茫月
    2021-01-30 16:33

    There are three ways a form may be evaluated in Scheme:

    1. Applicative Order
      • evaluate the arguments and then apply
      • given f(x)=x+x: 3*f(1)*f(1)3*2*2
    2. Normal Order
      • fully expand and then reduce
      • given f(x)=x+x: 3*f(1)*f(1)3*(1+1)*(1+1) (also used in "lazy evaluation")
    3. Special Forms For example:
      • Booleans and and or. For example: (and ... ) evaluates Left → Right. If any evaluates to false, the value of the and expression is false, and the rest of the 's are not evaluated.
      • Conditionals like if and cond
        • (if ): If the evaluates to a true value, the interpreter then evaluates the and returns its value. Otherwise it evaluates the and returns its value
        • (cond ( ) ... ( )): The predicate is evaluated first. If its value is false, then is evaluated. If 's value is also false, then is evaluated. When true predicate, the interpreter returns the value of the corresponding consequent expression .

    In the case of Exercise 1.6:

    • new-if is a normal procedure. In Scheme (and many other languages), arguments are fully evaluated before the procedure is called. This is known as applicative order. ∴ sqrt-iter is called every time new-if is called, resulting in an infinite loop.
    • For the reader's edification, normal ifs are a special form. The recursive statement wouldn't be evaluated unless the is called.

提交回复
热议问题