<< Previous exercise (1.45) | Index | Next exercise (2.1) >>

Note that the arguments to *iterative-improve* must:

- tell if
*a guess*is**good enough** - improve a guess

(define (iterative-improve good-enough? improve) (lambda (guess) (if (good-enough? guess) guess ((iterative-improve good-enough? improve) (improve guess))))) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (fixed-point f first-guess) ((iterative-improve (lambda (x) (close-enough? x (f x))) f) first-guess)) (define (sqrt x) ((iterative-improve (lambda (y) (< (abs (- (square y) x)) 0.0001)) (lambda (y) (average y (/ x y)))) 1.0))

Here is a cleaner version of *iterative-improve* that reduces the number of state variables from three to one by using an inner procedure *iter*.

```
(define (iterative-improve good-enough? improve)
(lambda (first-guess)
(define (iter guess)
(if (good-enough? guess)
guess
(iter (improve guess))))
(iter first-guess)))
```

You can make it even cleaner by simply returning the inner procedure

```
(define (iterative-improve good-enough? improve)
(lambda (first-guess)
(define (iter guess)
(if (good-enough? guess)
guess
(iter (improve guess))))
iter))
```

You can make it even cleaner by removing the unnecessary lambda

```
(define (iterative-improve good-enough? improve)
(define (iter guess)
(if (good-enough? guess)
guess
(iter (improve guess))))
iter)
```

- The use of inner procedure avoids call of `iterative-improve` in second and after recursion.

- And the inner procedure captures `good-enough?` `improve` variables in `iterative-improve` thus return inner procedure is just ok.

- And the name of inner procedure is just for referencing itself in recursion.

"You can make it even cleaner by simply returning the inner procedure" is one wrong way to "return inner procedure" since `lambda` will take the parameter in and then return the inner procedure

without calling itwith the parameter. Contrary to that, "You can make it even cleaner by removing the unnecessary lambda" will work since `iter` will directly take the parameter in. In a summary, the differences between these 2 are about syntax and it is ok to ignore it since the book is not to emphasize the Scheme syntax.