Lisp Formatting Polynomial

谁说胖子不能爱 提交于 2019-12-06 02:07:04

Answer:

I would not put this logic into FORMAT statements. Only if you want to encrypt your code or create more maintenance work for yourself. Good Lisp code is self-documenting. FORMAT statements are never self-documenting.

Before printing I would first simplify the polynomial. For example removing every term which is multiplied by zero.

((0 10) (1 2)) -> ((1 2))

Then if the multiplier is 1 can be tested in a normal COND or CASE statement.

Also make sure that you never use CAR, CDR, FIRST, SECOND with a self-made data structure. The components of a polynomial should mostly be accessed by self-documenting functions hiding most of the implementation details.

I would write it without FORMAT:

Example code:

(defun term-m (term)
  (first term))

(defun term-e (term)
  (second term))

(defun simplify-polynomial (p)
  (remove-if #'zerop (sort p #'> :key #'term-e)
             :key #'term-m))

(defun write-term (m e start-p stream)
  ; sign or operator
  (cond ((and (minusp m) start-p)
         (princ "-" stream))
        ((not start-p)
         (princ (if (plusp m) " + " " - ") stream)))
  ; m
  (cond ((not (= (abs m) 1))
         (princ (abs m) stream)))
  (princ "x" stream)
  ; e
  (cond ((not (= 1 e))
         (princ "^" stream)
         (princ e stream))))

(defun write-polynomial (p &optional (stream *standard-output*))
  (loop for (m e) in (simplify-polynomial p)
        for start-p = t then nil
        do (write-term m e start-p stream)))

Example use:

CL-USER 14 > (write-polynomial '((1 2) (3 6) (-20 48)))
-20x^48 + 3x^6 + x^2
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!