【发布时间】:2015-08-13 06:10:56
【问题描述】:
我正在研究旧考试,为自己的考试做准备,教授很友善,还为我们提供了解决方案,现在我想知道为什么一个函数会做它应该做的事情。
(defun sortulists (L)
(mapcar (lambda (uliste)
(sort uliste (lambda (x1 x2)
(or (symbolp x2)
(and (numberp x1) (numberp x2)
(< x1 x2))))))
L))
它应该获取一个列表L,其中包含可能包含数字和原子的未排序子列表,并首先对其数字进行排序,然后将符号放在末尾。
当像这样调用(sortulists '((A 9 b h 2) (1 m n 9 8) (5 a 7))) 时,它会返回((2 9 H B A) (1 8 9 N M) (5 7 A))。
有什么帮助吗?
编辑:固定缩进
【问题讨论】:
-
你不明白的部分有什么精确的吗?
-
我的问题是理解
(or symbolp x2) (and (numberp x1) (numberp x2) (< x1 x2))))))部分。我知道< x1 x2是实际排序,但我对or和and感到困惑。是符号还是排序后的数字? -
请注意,此谓词导致符号的顺序不可预测。此外,您的示例调用将文字列表传递给
sortulists,这会将内部文字列表传递给sort, which is allowed to destructively modify the sequence。这意味着undefined behavior。
标签: sorting lisp common-lisp