We are using here a powerful strategy for designing programs: wishful
thinking. We haven't yet said how a rational number is represented, or how the
functions numer , denom , and rational should be implemented. Even
so, if we did define these three functions, we could then add, multiply, print,
and test equality of rational numbers:
If you want to change selection, open document below and click on "Move attachment"
2.2 Data Abstractions the following three functions:
rational(n, d) returns the rational number with numerator n and
denominator d . numer(x) returns the numerator of the rational number x . denom(x) returns the denominator of the rational number x .
<span>We are using here a powerful strategy for designing programs: wishful
thinking. We haven't yet said how a rational number is represented, or how the
functions numer , denom , and rational should be implemented. Even
so, if we did define these three functions, we could then add, multiply, print,
and test equality of rational numbers:
>>> def add_rationals(x, y):
nx, dx = numer(x), denom(x)
ny, dy = numer(y), denom(y)
return rational(nx * dy + ny * dx, dx * dy)
>>> def