发一帖,抛砖引玉,冒泡法。

- ;;比较两个数值表的大小,fun可以是“'<”(升序)或“'>”(降序)
- (defun comp_list (list1 list2 fun / sign i len1 len2 listt list_c list_a)
- (setq sign nil
- i 0
- len1 (length list1)
- len2 (length list2)
- n (eval (list 'min len1 len2))
- list_a (list list1 list2)
- )
- (if (not (eval (list fun len1 len2)))
- (setq listt list1
- list1 list2
- list2 listt
- )
- )
- (setq list_c (list list1 list2))
- (while (and (not sign) (< i n))
- (setq atom1 (nth i list1)
- atom2 (nth i list2)
- )
- (if (= atom1 atom2)
- (setq i (1+ i))
- (if (eval (list fun atom1 atom2))
- (setq sign t)
- (setq sign t
- list_c (list list2 list1)
- )
- )
- )
- )
- ; (princ list_c) ;返回排序后组合表
- (if (equal list_c list_a) ;返回是否符合函数fun
- t
- nil
- )
- )
- ;;二阶数值表排序,fun可以是“'<”(升序)或“'>”(降序)
- (defun sort_list (nlist fun / len lista list_b list_o)
- (setq list_o nil
- len (length nlist)
- )
- (repeat len
- (setq lista (car nlist)
- nlist (cdr nlist)
- list_b nil
- )
- (mapcar
- '(lambda (x)
- (if (comp_list lista x fun)
- (setq list_b (cons x list_b))
- (setq list_b (cons lista list_b)
- lista x
- )
- )
- )
- nlist
- )
- (setq nlist list_b)
- (setq list_o (cons lista list_o))
- )
- (reverse list_o)
- )
测试:

- 命令: (setq nlist '((1 2 3) (3 3) (3 1 2 4) (2 1 3) (3 3 3) (2 1 3 4) (2 2) (1 2) (1 2 3 4 5)))
- ((1 2 3) (3 3) (3 1 2 4) (2 1 3) (3 3 3) (2 1 3 4) (2 2) (1 2) (1 2 3 4 5))
- 命令: (sort_list nlist '<)
- ((1 2) (1 2 3) (1 2 3 4 5) (2 1 3) (2 1 3 4) (2 2) (3 1 2 4) (3 3) (3 3 3))
- 命令: (sort_list nlist '>)
- ((3 3 3) (3 3) (3 1 2 4) (2 2) (2 1 3 4) (2 1 3) (1 2 3 4 5) (1 2 3) (1 2))
|