浏览代码

Streamline code, and sort of bug fix

Matt Coles 10 年之前
父节点
当前提交
510a6535e3
共有 1 个文件被更改,包括 12 次插入7 次删除
  1. 12 7
      poly.cl

+ 12 - 7
poly.cl

@@ -1,10 +1,10 @@
1 1
 ;; Add two polynomials together
2 2
 (defun poly+ (poly1 poly2)
3
-  (add-terms (append poly1 poly2) nil nil))
3
+  (add-terms (append poly1 poly2)))
4 4
 
5 5
 ;; Subtract one polynomial from another
6 6
 (defun poly- (poly1 poly2)
7
-  (add-terms (append poly1 (flip-multipliers poly2)) nil nil))
7
+  (add-terms (append poly1 (flip-multipliers poly2))))
8 8
 
9 9
 ;; Returns the variable component of a singular polynomial
10 10
 (defun variable-symbol (single-poly)
@@ -39,6 +39,13 @@
39 39
     nil
40 40
     term2))
41 41
 
42
+;; Returns true if a polynomial contains only unique terms
43
+(defun poly-unique-terms? (poly)
44
+  (cond ((equal poly nil) T)
45
+  (T (if (some #'(lambda (x) (equal (car x) (car (car poly)))) (cdr poly))
46
+    nil
47
+    (poly-unique-terms? (cdr poly))))))
48
+
42 49
 ;; This function replaces a full list of nils with the replace-term, otherwise
43 50
 ;; returns the list
44 51
 (defun useful-replace-nil (addition-seq replace-term)
@@ -52,17 +59,15 @@
52 59
                  (if (equal (multiplier x) 0) nil x)) poly)))
53 60
 
54 61
 ;; Recursively adds all the terms in the list
55
-(defun add-terms (poly orig_var curr_var)
62
+(defun add-terms (poly)
56 63
   (cond
57
-    ((and (equal orig_var curr_var) (not (equal orig_var nil))) (clear-zero poly))
64
+    ((poly-unique-terms? poly) (clear-zero poly))
58 65
     (T (add-terms (remove nil (append (map 'list #'(lambda (x)
59 66
                                      (term-failure (car poly) x))
60 67
                            (cdr poly))
61 68
         (useful-replace-nil (map 'list #'(lambda (y)
62 69
                        (term-addition (car poly) y))
63
-             (cdr poly)) (car poly)))) 
64
-                  (if (equal orig_var nil) (car (car poly)) orig_var)
65
-                  (car (car (cdr poly)))))))
70
+             (cdr poly)) (car poly))))))))
66 71
 
67 72
 ; (poly+ '(((x 2) 3)) '(((y 2) 4)))
68 73
 ; (poly+ '(((x 2) 3) ((y 2) 3)) '(((y 2) 4)))