Recursion: Stopping Criterion

Sometimes a stopping criterion other than a simple counter is used. For example, we could test whether an answer is ``good enough'':


(define (mysqrt x) (mysqrtb x 1.0 1.0e-8))

(define (mysqrtb x estimate epsilon) (if (good-enough x (square estimate) epsilon) estimate (mysqrtb x (new-estimate x estimate) epsilon) ) )

(define (square x) (* x x))

(define (good-enough x y epsilon) (< (abs (- x y)) epsilon))

(define (new-estimate x estimate) (/ (+ estimate (/ x estimate)) 2))

The question of whether a program will terminate is an important one; sometimes it is difficult to answer.

Contents    Page-10    Prev    Next    Page+10    Index