SICP 2.5 exercises cont. 4

2.90

This took a really long time. Not only was I very busy with school and work, but also I kept getting lost in my code when I could actually spare some time. Here I repost a lot of the code from the previous exercise. Changes include the addition of public make polynomial methods, and new procedures and tags to the dense and sparse polynomial packages. Finally a general install-polynomial package is implemented

(define (make-dense-polynomial var terms)
  ((get 'make-dense 'polynomial) var terms))

(define (make-sparse-polynomial var terms)
  ((get 'make-sparse 'polynomial) var terms))

(define (install-sparse-polynomial-package)
  (define (add-poly p1 p2)
    (display (list (variable p1) (variable p2)))
    (newline)
    (if (same-variable? (variable p1) (variable p2))
        (make-poly (variable p1)
                   (add-terms (term-list p1)
                              (term-list p2)))
        (error "Polys not in same var -- ADD-POLY"
               (list p1 p2))))
  (define (mul-poly p1 p2)
    (if (same-variable? (variable p1) (variable p2))
        (make-poly (variable p1)
                   (mul-terms (term-list p1)
                              (term-list p2)))
      (error "Polys not in same var -- MUL-POLY"
             (list p1 p2))))

  ;; internal procedures
  ;; representation of poly
  (define (make-poly variable term-list)
    (cons variable term-list))
  (define (variable p) (car p))
  (define (variable? x) (symbol? x))
  (define (term-list p) (cdr p))
  (define (same-variable? v1 v2)
    (and (variable? v1) (variable? v2) (eq? v1 v2)))

  (define (adjoin-term term term-list)
    (if (=zero? (coeff term))
        term-list
      (cons term term-list)))
  (define (the-empty-termlist) '())
  (define (first-term term-list) (car term-list))
  (define (rest-terms term-list) (cdr term-list))
  (define (empty-termlist? term-list) (null? term-list))
  (define (make-term order coeff) (list order coeff))
  (define (order term) (car term))
  (define (coeff term) (cadr term))

  (define (add-terms L1 L2)
    (cond ((empty-termlist? L1) L2)
          ((empty-termlist? L2) L1)
          (else
           (let ((t1 (first-term L1)) (t2 (first-term L2)))
             (cond ((> (order t1) (order t2))
                    (adjoin-term
                     t1 (add-terms (rest-terms L1) L2)))
                   (( (length L1) (length L2)) (adjoin-term (first-term L1) (add-terms (rest-terms L1) L2)))
          ((< (length L1) (length L2)) (adjoin-term (first-term L2) (add-terms L1 (rest-terms L2))))
          (else
           (let ((t1 (first-term L1)) (t2 (first-term L2)))
             (adjoin-term (+ t1 t2) (add-terms (rest-terms L1) (rest-terms L2)))))))

  (define (mul-terms L1 L2)
    (if (empty-termlist? L1)
        (the-empty-termlist)
      (add-terms (mul-term-by-all-terms (length L1) (car L1) L2)
                 (mul-terms (rest-terms L1) L2))))

  (define (mul-term-by-all-terms t1-order t1-coeff L)
    (cond ((and (= t1-order 1) (empty-termlist? L)) (the-empty-termlist))
          ((empty-termlist? L) (cons 0 (mul-term-by-all-terms (- t1-order 1) t1-coeff L)))
          (else (cons (* t1-coeff (car L)) (mul-term-by-all-terms t1-order t1-coeff (cdr L))))))

  (define (zero-terms? term-list)
    (cond ((empty-termlist? term-list) #t)
          ((not (= 0 (coeff (first-term term-list)))) #f)
          (else (zero-terms? (rest-terms term-list)))))

  (define (negate-terms term-list)
    (if (empty-termlist? term-list)
        (the-empty-termlist)
        (cons (- (first-term term-list))
              (negate-terms (rest-terms term-list)))))
  (define (negate-poly p)
    (make-polynomial (variable p) (negate-terms (term-list p))))
  ;; interface to rest of the system
  (define (tag p) (attach-tag 'dense p))
  (put 'add '(dense dense)
       (lambda (p1 p2) (tag (add-poly p1 p2))))
  (put 'mul '(dense dense)
       (lambda (p1 p2) (tag (mul-poly p1 p2))))
  (put 'make 'dense
       (lambda (var terms) (tag (make-poly var terms))))
  (put '=zero? '(dense) (lambda (p) (zero-terms? (term-list p))))
  (put 'negate '(dense) negate-poly)
  'done)

(define (install-polynomial-package)
  (define (make-dense-proc var terms)
    ((get 'make 'dense) var terms))
  (define (make-sparse-proc var terms)
    ((get 'make 'sparse) var terms))
  (define (tag p) (attach-tag 'polynomial p))
  (put 'add '(polynomial polynomial)
       (lambda (p1 p2) (tag (add p1 p2))))
  (put 'mul '(polynomial polynomial)
       (lambda (p1 p2) (tag (mul p1 p2))))
  (put 'sub '(polynomial polynomial)
       (lambda (p1 p2) (tag (sub p1 p2))))
  (put 'negate '(polynomial) negate)
  (put 'make-dense 'polynomial
       (lambda (var terms) (tag (make-dense-proc var terms))))
  (put 'make-sparse 'polynomial
       (lambda (var terms) (tag (make-sparse-proc var terms)))))

The results are what I wanted but I am quite muddled. Going to move on but perhaps I’ll revisit this later.

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s