Sort polynomials Common Lisp

空扰寡人 提交于 2019-12-02 02:27:30

Your definition of what you want to do is sufficiently opaque that an answer is hard to provide. But the way to start is to stop programming like it is 1956 and use some abstraction.

First of all, let's define how to make a variable and get at its bits:

(defun make-variable (name &optional (degree 1))
  `(v ,name ,degree))

(defun variable-name (v)
  (second v))

(defun variable-degree (v)
  (third v))

Now let's define how to make polynomials from lists of variables. Note that the total degree of the polynomial is computable from the degrees of all the variables, so we do that.

(defun make-polynomial (variables &optional (coefficient 1))
  ;; The total degree of the polynomial can just be computed from the
  ;; degrees of its variables
  `(m ,coefficient ,(reduce #'* variables :key #'variable-degree)
      ,variables))

(defun polynomial-coefficient (p)
  (second p))

(defun polynomical-total-degree (p)
  (third p))

(defun polynomial-variables (p)
  (fourth p))

Now, given lists of polynomials, we can sort them using the abstractions we've built: we don't need to grovel around with list accessors (and indeed we could change the representation of polynomials or variables and nothing would ever know).

I am guessing that what you want to sort on is the highest degree of a variable in a polynomial although it is not really clear, and not the total degree of the polynomial (which would be easier). So let's write a function to pull out the highest variable degree:

(defun highest-variable-degree (p)
  (reduce #'max (mapcar #'variable-degree (polynomial-variables p))))

And now we can sort lists of polynomials.

CL-USER 23 > (sort (list (make-polynomial (list (make-variable 'a)
                                               (make-variable 'b 2)))
                         (make-polynomial (list (make-variable 'c)
                                                (make-variable 'd))))
                   #'<
                   :key #'highest-variable-degree)
((m 1 1 ((v c 1) (v d 1))) (m 1 2 ((v a 1) (v b 2))))

Remember: it is not 1956 any more.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!