MRA*_*ing 5 lisp sorting common-lisp
如何使用子列表对列表进行排序?
(setq list '((0) (1) (2) (0 1 5) (0 1 3) (0 1 5) (0 3 0) (0) (1)
(2 7 19) (0 0 3 0)))
; restricting the sort to only the first element:
(sort (copy-seq list) #'< :key #'car)
--> ((0) (0 1 5) (0 1 3) (0 1 5) (0 3 0) (0) (0 0 3 0) (1) (1) (2) (2 7 19))
Run Code Online (Sandbox Code Playgroud)
我正在寻找的输出是对子列表的所有元素排序:
--> ((0) (0) (0 0 3 0) (0 1 3) (0 1 5) (0 1 5) (0 3 0) (1) (1) (2) (2 7 19))
Run Code Online (Sandbox Code Playgroud)
WRe*_*ach 12
首先定义一个函数,确定一个列表是否小于另一个列表.以下示例假定列表只能包含数字:
(defun list< (a b)
(cond ((null a) (not (null b)))
((null b) nil)
((= (first a) (first b)) (list< (rest a) (rest b)))
(t (< (first a) (first b))) ))
Run Code Online (Sandbox Code Playgroud)
使用此功能,您现在可以对列表列表进行排序.
(sort (copy-seq list) #'list<)
Run Code Online (Sandbox Code Playgroud)