Сложение многочленов
(defmethod add2 ((p1 polynom) (p2 polynom))
(if (same-variable-p (var p1) (var p2))
(make-instance 'polynom
:var (var p1)
:terms (add-terms (terms p1)
(terms p2)))
(error "Многочлены от разных переменных: ~s и ~s"
p1 p2)))
(defun same-variable-p (v1 v2)
(and (symbolp v1) (symbolp v2) (eq v1 v2)))
(defun add-terms (tl1 tl2)
(cond ((null tl1) tl2)
((null tl2) tl1)
(t
(let ((t1 (first tl1))
(t2 (first tl2)))
(cond ((> (order t1) (order t2))
(adjoin-term t1
(add-terms (rest tl1) tl2)))
((< (order t1) (order t2))
(adjoin-term t2
(add-terms tl1 (rest tl2))))
(t
(adjoin-term
(make-term :coeff (add2 (coeff t1) (coeff t2))
:order (order t1))
(add-terms (rest tl1)
(rest tl2)))))))))
(defun adjoin-term (term term-list)
(if (zerop1 (coeff term))
term-list
(cons term term-list)))
(add2 p1 p2)
[МЧ (X) +1X^3+5X^2+5.3X-6]