Classificar polinômios Lisp comum

Estou tentando classificar uma lista de polinômios escritos neste formato: (M [coeficiente] [grau total] [Lista de variáveis]).

exemplo:

((M 1 1 ((V 1 A))) (M 1 2 ((V 1 A) (V 1 C))) (M 1 2 ((V 2 A))) (M 1 2 ((V 1 A) (V 1 B))))

Isto é: a + a * c + a ^ 2 + a * b, preciso obter a + a * b + c + a * a ^ 2, porque a * b <a ^ 2 e a <a ^ 2.

Eu tentei usar a classificação da função, mas minha saída é:

((M 1 1 ((V 1 A))) (M 1 2 ((V 2 A))) (M 1 2 ((V 1 A) (V 1 B))) (M 1 2 ((V 1 A) (V 1 C))))

isto é a + a ^ 2 + a * b + a * c.

Eu uso:

(defun sort-poly (a b)
  (cond 
    (t (sort-poly-helper (varpowers a) (varpowers b)))))

(defun sort-poly-helper (a b)
  (cond 
    ((null a) (not (null b)))
    ((null b) nil)
    ((equal (third(first a)) (third(first b))) (sort-poly-helper (rest a) (rest b)))
    (t (sort (list (third(first a)) (third(first b))) #'string-lessp))))

com:

 (sort '((M 1 1 ((V 1 A))) (M 1 2 ((V 1 A) (V 1 C))) (M 1 2 ((V 2 A))) (M 1 2 ((V 1 A) (V 1 B)))) #'sort-poly)

Alguma ajuda? obrigado

questionAnswers(1)

yourAnswerToTheQuestion