From: Ramakrishnan Muthukrishnan Date: Sat, 1 Jan 2011 07:31:31 +0000 (+0530) Subject: solution to 2.91 X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/%22news.html/%22file:/nxhtml.html?a=commitdiff_plain;h=9e1ba29f8067baf0f79fd3ea9d64a8f54548d560;p=sicp.git solution to 2.91 --- diff --git a/src/sicp/ex2_91.rkt b/src/sicp/ex2_91.rkt new file mode 100644 index 0000000..a0a7462 --- /dev/null +++ b/src/sicp/ex2_91.rkt @@ -0,0 +1,29 @@ +#lang racket + +(define (div-terms L1 L2) + (if (empty-termlist? L1) + (list (the-empty-termlist) (the-empty-termlist)) + (let ((t1 (first-term L1)) + (t2 (first-term L2))) + (if (> (order t2) (order t1)) + (list (the-empty-termlist) L1) + (let ((new-c (div (coeff t1) (coeff t2))) + (new-o (- (order t1) (order t2)))) + (let ((rest-of-result + (div-terms (sub-terms L1 + (mul-term-by-all-terms (make-term new-o new-c) + L2)) + L2))) + (list (adjoin-term (make-term new-o new-c) + (car rest-of-result)) + (cadr rest-of-result)))))))) + +(define (div-poly P1 P2) + (if (same-variable? (variable P1) + (variable P2)) + (let ((t1 (term-list P1)) + (t2 (term-list P2))) + (let ((div-results (div-terms t1 t2))) + (list (make-poly (variable P1) (car div-results)) + (make-poly (variable P1) (cadr div-results))))) + (error "Polynomials are not of the same variable -- DIV-POLY"))) \ No newline at end of file