0

My basic problem is that I have a list of lists that represent polynominals functions and I want to sort them based on the variable and then the exponent.

So for example variable 'x' before variable 'y' and variable 'x^n' before variable 'x^n-1'.

I have a function 'merge' which with the following input:

(merge '((5 x 2)(3 x 2)(10 x 2)(15 x 2)(20 x 2)))

Will result in this output

(53 X 2)

53 is the coefficient, x is the variable and 2 is the exponent. I can sort by the variable to get first all of the a's and then b's and then c's etc. but I don't know how to switch around the exponents afterwards.

4

1 に答える 1

3

並べ替える基準が 2 つある場合は、最初に 2 番目の基準で並べ替え、次に 1 番目の基準で安定して並べ替えることができます。

(stable-sort (sort polynomial #'< :key #'third)
             #'string<
             :key #'second)

string<(シンボルはstring designatorsであるため、シンボルを と比較できます。)

順序を定義するより複雑な関数がある場合は、次のように個別に定義します。

(sort polynomial #'term<)

(defun term< (term0 term1)
  (if (string= (second term0) (second term1))
      (< (third term0) (third term1))
      (string< (second term0) (second term1))))
于 2014-12-09T14:44:27.727 に答える