From jsmith@iastate.edu Tue Oct 26 08:54:22 1993
Return-Path: <jsmith@iastate.edu>
To: leavens@iastate.edu
Cc: kishore@iastate.edu
Subject: print-poly.ss
Date: Tue, 26 Oct 93 06:59:47 CDT
From: Jason A Smith <jsmith@iastate.edu>

I have enclosed a version of print-poly that I call perfect. It prints a
polynomial exacly as a math professor would (and I should know because I used
to be a math major!).

1) It won't print a plus for the first term.

2) It won't print "+ - 17.0 X" for a negative term. It only prints one operand
per term.

3) It won't print a 1 coefficient. It will print X^2 instead of 1*X^2.

4) It is representation indepentent. (The closest there is to an exeption is
that it uses (equal? the-zero-poly poly1) instead of (zero-poly? poly1). But I
ran into problems with different representations in the library creating
errors on that procedure. I think it was because I din't load ch5.ss but
instead went right to a polynomial file. I'm not exactly sure, but I do know
that the zero-polynomial? function almost always creates errors if you load a
polynomial representation directly instead of going throught ch5.ss. It was my
judgement that representation independence was actually helped through use of
my method rather than the library call.

I think you may want to consider replacing the print-poly file in the library
with my version. It will always present a representation that algebra students
are accustomed to.


;;           NAME: Jason Smith           TA: Kishore      Sect: W3

;;           Homework   5                 Problem number   18

;;           Source Code file

;;           Edited on MacGambit Scheme interpreter for the Macintosh



(define print-poly
  (lambda (poly)
    (if (zero? (degree poly))
      (writeln (leading-coef poly))
      (letrec ((print-helper
                (lambda (p)
                  (if (equal? the-zero-poly p)
                    (newline)
                    (begin
                      (cond
                       ((negative? (leading-coef p)) (display " - "))
                       ((< (degree p) (degree poly)) (display " + ")))
                      (if (zero? (degree p))
                        (begin
                          (display (abs (leading-coef p)))
                          (newline))
                        (begin
                          (if (not (= 1 (abs (leading-coef p))))
                            (display (abs (leading-coef p))))
                          (display 'x)
                          (if (< 1 (degree p))
                            (begin
                              (display '^)
                              (display (degree p))))
                          (print-helper (rest-of-poly p)))))))))
               (print-helper poly)))))

 


