2012-09-07

SICP Exercise 2.92: Quick Update

If you remember, back in what I called the Naïve Approach, I gave an example subtraction of two polynomials:
((5y2 + 2y - 1)x2 + (2y2 + y + 2)x - 3) - ((5x2 + 2x)y2 + (2x2 + x)y - (x2 - 2x + 5))
...and showed how the naïve approach (i.e. expressing one polynomial in the same indeterminate as the other by simply creating a new polynomial of the required indeterminate with only a zero-order term with the coefficient being the first polynomial) failed to correctly simplify the results, giving:
((5y2 + 2y - 1)x2 + (2y2 + y + 2)x - ((5x2 + 2x)y2 + (2x2 + x)y - (x2 - 2x - 2))
...instead of:
2
Well, here's the output from my Scheme interpreter this morning:
> (define poly-1
    (make-polynomial-from-coeffs
     'x
     (list (make-polynomial-from-coeffs
            'y
            (list (make-integer 5) (make-integer 2) (make-integer -1)))
           (make-polynomial-from-coeffs
            'y
            (list (make-integer 2) (make-integer 1) (make-integer 2)))
           (make-integer -3))))
> (define poly-2
    (make-polynomial-from-coeffs
     'y
     (list (make-polynomial-from-coeffs
            'x
            (list (make-integer 5) (make-integer 2) zero))
           (make-polynomial-from-coeffs
            'x
            (list (make-integer 2) (make-integer 1) zero))
           (make-polynomial-from-coeffs
            'x
            (list (make-integer -1) (make-integer 2) (make-integer -5))))))
> (sub poly-1 poly-2)
(integer . 2)
Write-up to follow. Unfortunately I'm away this weekend, so it may not be posted until sometime next week. However, I'm pretty chuffed I've finally found the time to finish this exercise off!

No comments:

Post a Comment