Paul Wallich
2/2/2016 2:38:00 PM
On 2/2/16 9:12 AM, franklin wrote:
> This is a quote from SICP about coercion. This section talks about the arithmetic package with ordinary numbers, rational numbers and complex numbers and doing cross type operations on them (E.G. adding a complex number to an ordinary number.)
>
> "This coercion scheme has many advantages over the method of defining explicit cross-type operations, as outlined above. Although we still need to write coercion procedures to relate the types (possibly N^2 procedures for a system with N types), we need to write only one procedure for each pair of types rather than a different procedure for each collection of types and each generic operation."
>
> I'm confused on this line:
>
> "possibly N^2 procedures for a system with N types"
>
> Let's take the arithmetic package example. The operations that deel with two ordinary numbers (scheme-number scheme-number), two rational numbers (rational rational) and two complex numbers (complex complex) are the same types, so they are not included in the coercion procedures.
>
> We have three types, these are the coercion procedures I can think of with just two arguments.
>
> (scheme-number rational)
> (scheme-number complex)
> (rational scheme-number)
> (rational complex)
> (complex scheme-number)
> (complex rational)
>
> These are not n^2 coercion procedures. There are only six coercion procedures here, not 9. I think I'm not really understanding this part of the text at all. Can someone explain what I missed?
I think you may be missing the coercions of a type to itself. Yeah, not
a very interesting operation, but from the point of view of a complete
system, it still has to be able to recognize "Oh, this number is already
of the type desired, so I don't do anything."
If you leave out the identity coercion, as you keep increasing the
number of types, you see the number of coercions needed is n*(n-1), and
a you head for infinity, that's going to get closer and closer to n^2.
But I doubt SICP would pull that.