Iterative.

Recursive.

## Friday, April 25, 2008

## Wednesday, April 23, 2008

### Exercise 1.7

"The good-enough? test used in computing square roots will not be very effective for finding the square roots of very small numbers." If x is much smaller than 0.001, good-enough? will return true for numbers that are significantly off. For example, it will return true if x is 0.000001 and guess is 0.0001, (abs (- (square guess) x)) = 0.00000999... and good-enough? returns true, even though the guess is off by a factor of 100.

"Also, in real computers, arithmetic operations are almost always performed with limited precision. This makes our test inadequate for very large numbers." Limited precision means that there is a limit to the number of significant figures in our calculations. Suppose the limit is 7 significant figures, x is 10000000000000000000000000000000000000000000000000000 and guess is 100000000000000000001000000. (abs (- (square guess) x)) = 0 and good-enough? returns true, even though guess is off by 1000000.

"Also, in real computers, arithmetic operations are almost always performed with limited precision. This makes our test inadequate for very large numbers." Limited precision means that there is a limit to the number of significant figures in our calculations. Suppose the limit is 7 significant figures, x is 10000000000000000000000000000000000000000000000000000 and guess is 100000000000000000001000000. (abs (- (square guess) x)) = 0 and good-enough? returns true, even though guess is off by 1000000.

(define (good-enough? guess old-guess)

(< (abs (- guess old-guess)) (* 0.001 guess)))

(define (sqrt-iter guess x)

(if (good-enough? guess (improve guess x))

(improve guess x)

(sqrt-iter (improve guess x) x)))

### Exercise 1.6

The program will not halt. The first call to new-if will start by evaluating both arguments. But the second argument (the sqrt-iter call) calls new-if, which starts by evaluating both arguments. But again, the second argument (the sqrt-iter call) calls new-if, which starts by evaluating both arguments. And so forth.

## Friday, April 18, 2008

### Exercise 1.5

If the interpreter uses applicative-order evaluation, it will go into an infinite loop when it evaluates the second argument (p). This does (p), which does (p), and so on.

If the interpreter uses normal-order evaluation, it will not begin by evaluating the second argument. It executes the test expression with the unevaluated arguments. Because the first argument is 0, the result is 0; the second argument is left unevaluated, and no infinite loop occurs.

If the interpreter uses normal-order evaluation, it will not begin by evaluating the second argument. It executes the test expression with the unevaluated arguments. Because the first argument is 0, the result is 0; the second argument is left unevaluated, and no infinite loop occurs.

### Exercise 1.4

If b is greater than 0, the if expression returns +; otherwise it returns -. The resulting operator is used in the outer expression (operator a b). Thus, (- a b) if b is negative; (+ a b) if b is positive.

Subscribe to:
Posts (Atom)