Evi*_*Mer 4 lisp sorting common-lisp
我正在做旧考试,为我自己的考试做好准备,教授也很好,也给了我们解决方案,现在我想知道为什么一个函数做了它应该做的事情.
(defun sortulists (L)
(mapcar (lambda (uliste)
(sort uliste (lambda (x1 x2)
(or (symbolp x2)
(and (numberp x1) (numberp x2)
(< x1 x2))))))
L))
Run Code Online (Sandbox Code Playgroud)
它应该采用一个列表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)).
有帮助吗?
编辑:修复缩进
该sort函数的谓词 表明一旦序列被排序,测试必须为真.如何定义排序.
如果你挣扎 and,并or为他们在这里使用的,我建议你阅读的章节标记条件的Common Lisp的:一个温柔的介绍符号计算.它展示了如何互换cond,嵌套ifS和组合and和or,并提供运动(和他们的解决方案).
简而言之,要么右边有一个符号,要么两个都是数字,它们必须按大小排序.
(or
; if x2 is a symbol, then x1 is smaller, whatever x1 is
(symbolp x2)
; if both are numbers, then return t if x1 is smaller than x2
(and (numberp x1) (numberp x2)
(< x1 x2)))
Run Code Online (Sandbox Code Playgroud)
所以数字在前面排序.符号在最后,但未分类.