SICP Exercise 2.3 make-rectangle 1

Exercise 2.3.  Implement a representation for rectangles in a plane. (Hint: You may want to make use of exercise 2.2.) In terms of your constructors and selectors, create procedures that compute the perimeter and the area of a given rectangle. Now implement a different representation for rectangles. Can you design your system with suitable abstraction barriers, so that the same perimeter and area procedures will work using either representation?

SOLUTION

In this implementation, a rectangle is represented by its two diagonals. It is constructed by supplying two segments to it which will be its diagonals. If the segments do not satisfy the conditions to be diagonals of a rectangle, an error will be reported.

The code and tests are here.

Comments

Popular posts from this blog

SICP Exercise 2.56 differentiation rule

SICP Exercise 1.28 (Miller-Rabin Test)

SICP Exercise 4.18 a alternative strategy for interpreting internal definitions