[plt-scheme] HtDP 27.3.2 and 27.3.4

From: David Yrueta (dyrueta at gmail.com)
Date: Sat Jul 4 01:34:06 EDT 2009

Hi All --

Questions for both exercises refer to the function "find-root" below:

;; find-root : (number  ->  number) number number  ->  number
;; to determine a number R such that f has a
;; root between R and (+ R TOLERANCE)
;;
;; ASSUMPTION: f is continuous and monotonic
(define (find-root f left right)
  (cond
    [(<= (- right left) TOLERANCE) left]
    [else
      (local ((define mid (/ (+ left right) 2)))
	(cond
	  [(<= (f mid) 0 (f right))
           (find-root f mid right)]
	  [else
           (find-root f left mid)]))]))

;; poly : number  ->  number
(define (poly x)
 (* (- x 2) (- x 4)))

HtDP exercise 27.3.2:

"Use poly from 27.3.1 to test find-root. Experiment with different
values for TOLERANCE. Use the strategy of section 17.8 to formulate
the tests as boolean-valued expressions."

Are these tests meant to take place inside or outside the body of
"find-root." In other words, are they conditional expressions inside
"find-root"?  If not, how are these tests different from a typical
"check-expect?"

(check-expect (find-root poly 3 6) 3.75)

HtDP exercise 27.3.4

"For every midpoint m, except for the last one, the function find-root
needs to determine the value of (f m) twice. Validate this claim for
one example with a hand-evaluation."

For the life of me, I don't see (f m) computed twice for every
midpoint.  "Mid" is obviously computed twice, but I only see "(f mid)"
computed once per call.  Am I missing something totally obvious?

Thanks!
Dave Yrueta


Posted on the users mailing list.