All pastes #1745751 Raw Edit

Anonymous

public text v1 · immutable
#1745751 ·published 2010-01-10 18:05 UTC
rendered paste body
(defun coef (t)
	(if (equal t ())
		()
		(car t)
	)
)
(defun var (t)
	(if (atom t)
		()
		(car (cdr t))
	)
)
(defun pow (t)
	(car (cdr (cdr t))))
(defun collectaux (p q) ;collects first "collecting" term of q with p
	(cond	((atom q)
		()) ;loop terminates here
		(atom (car q)
		())
		((equal p '())
		()) ;terminate here
		((and (equal (var p) (var (car q))) (equal (pow p) (pow (car q)))) ;if so, we can add them together
			(progn
				(print "add")
				(setq nonadder (cons 	nonadder 
							(cdr q)
						)
				);loop ends so we need to keep the terms we haven't added			
				(cons	(cons	(+ (coef p) (coef (car q))) ;add the coefficients
						(cons 	(var p) ;and append them with the var
							(cons (pow p) nil) ;and the pow
						) 
					)
					(collectaux p (cdr q))
				)

			)
			(progn
				(setq nonadder	(cons 	nonadder
							(car q)
					);car q won't add to p, so put it here
				)
				(cons nil (collectaux p (cdr q))) ;check through the rest of q
			)
		)
	)
)
(defun collectauxplus (p) ;this auxilary function runs collaux on our list until all terms have been added
		(if (equal p '())
			()
			(progn
				(setq nonadder ()) ;we keep the values that won't collect in here
				(setq collaux (collectaux (car p) (cdr p))) ;we will use this value more than once, so store it
				(if (equal collaux '())
					(cons 	(car p)
						(collectauxplus nonadder))
					(cons 	collaux 
						(collectauxplus nonadder))
				)
			)
		)
)
(defun collect (p) ;this runs collectauxplus again and again until we see no changes- i.e. we have fully collected our terms.
	(progn
		(setq cap (collectauxplus p)) ;load this value once as we use it twice
		(if (equal p cap)
			p
			(collect cap)
		)
	)
)