SICP exercise 2.10

From Drewiki

Jump to: navigation, search

Problem

Ben Bitdiddle, an expert systems programmer, looks over Alyssa's shoulder and comments that it is not clear what it means to divide by an interval that spans zero. Modify Alyssa's code from section 2.1.4 of the text to check for this condition and to signal an error if it occurs.

Solution

Let's signal an error by returning zero. Here are the unmodified procedures from Alyssa's original code in section 2.1.4 of the text:

 

Here's the modified div-interval procedure and a helper procedure that finds intervals that span zero (which may be useful to other procedure, so it's not an internal procedure to div-interval):

 

Test:

 

Output:

0.25
 

Output:

2.0
 

Output:

0
Personal tools