SICP Solutions

Section - 2.5 - Systems with Generic Operations

Exercise 2.86

Well, it turned out I missed few bugs in 2.85(which I have not fixed as it is part of this exercise). This exercise is in a way a part of previous exercise. As to complete that exercise complex number can have rational parts too, which was required because of the problems encountered when adding drop in that exercise.

Now, in this exercise this was asked explicitly to allow complex numbers with real and imaginary parts. I thought bingo!, I have already done it but the part that made me realised - I had left few issues - is that it explicitly asked for operation sin, cos. Soon I figured that I have not tested my last exercise with operations div and mul and used only the rectangular implementation of complex numbers. Thus in my test cases - sin and cos never came up!

Now, its just a matter of changing all operations used in rectangular and angular so that they can work fine.

Note that I have not defined sine, cosine and arctan by apply-generic because it can be done without it - just by raising the argument passed to scheme-number.

Also, I discovered one more bug in my code - again which should have been discovered earlier but because scheme is not static-typed - My implementation of angle was wrong as I forgot to call div and just passed two arguments - which a static language should have caught as I was passing two arguments where only one is expected. I have left that bug in the previous exercise.

So here goes the complete code:

Examples/Output: