All pastes #1746298 Raw Edit

Something

public text v1 · immutable
#1746298 ·published 2010-01-11 01:47 UTC
rendered paste body
(defun coef (t)
	(car t)
)
(defun var (t)
	(cadr t)
)
(defun pow (t)
	(caddr t))
(defun collectaux (L p) ;collects a new term to a collected list
(if (null L)
	(cons p nil)
(if (atom (car L))
	(cons p nil))
	(if (and (equal (var (car L)) (var p)) (equal (pow (car L)) (pow p))) ;if the terms can be collected (variables and powers the same)
		(append (cdr L)
			(cons	(+ (coef (car L)) (coef p))			;collect them (add coefficients and append)
				(cons	(var p)
					(cons	(pow p)
						nil
					)
				)
			)
		)
		(cons (car L) (collectaux (cdr L) p))				;keep running through the list
	)
)
)
(defun collect (L) ;collects a list using collectaux
(if (atom (car L))
	(cons L nil)
	(collectaux (collect (cdr L)) (car L))
)
)