Exercise 2.12 requires us to define a constructor make-center-percent, which receives two parameters, representing the center point and an error percentage, respectively. We need to use this constructor to generate a range. In addition, you need to define a selection function percent to get the error percentage of the specified range. In addition, you need to implement a center function to obtain the center point of the interval. The key here is
Exercise 2.12 requires us to define a constructor make-center-percent, which receives two parameters, representing the center point and an error percentage, respectively. We need to use this constructor to generate a range. In addition, you need to define a selection function percent to get the error percentage of the specified range. In addition, you need to implement a center function to obtain the center point of the interval.
The key here is to understand that a composite data type may have different forms. for example, the range can be expressed by the start and end points, or by the center point and error.
In terms of internal implementation, we can simply save the data related to the two forms, for example, we can record the start, end, center, and error of the interval at the same time. But in this way, we need two types of data at the same time when the data is updated. for example, if we get a new starting point and ending point data, we must update the starting point and ending point at the same time, center Point and error data, and the new center point and error can be calculated based on the new start point and end point.
Since all data needs to be converted, the other method of implementation is to retain only one form of data, and convert it to the first form of data through computation when obtaining another form of data.
Therefore, here we still use the data storage format of the previous start and end intervals, when we build an interval using make-center-percent, the center point and percentage of error are converted to the start point and end point by calculation.
The code is as follows:
(define (make-center-percent center percent) (make-interval (- center (* center (/ percent 100))) (+ center (* center (/ percent 100)))))
In this way, the code for getting the center point is the same as that in the book. after adding the start point and the end point, we can get the following code:
(define (center i) (/ (+ (lower-bound i) (upper-bound i)) 2))
When we know the start and end of an interval, we can calculate the width and get the error offset. then, we can get the percentage of the error relative to the center point by dividing the offset from the center point. Note that the width of the interval is defined in the book as half the distance from the start point to the end point, so the width of the interval here happens to be its error offset. The code for calculating the error percentage is as follows:
(define (percent i) (* 100 (abs (/ (interval-width i) (center i)))))
This completes exercise 2.12.