我正在尝试使用LISP进行快速排序,但我遇到了我的函数输出问题。LISP中的快速排序
(defun qsort (L)
(cond
((null L) nil)
(t(append
(qsort (list< (car L) (cdr L)))
(cons (car L) nil)
(qsort (list>= (car L) (cdr L)))))))
(defun list< (a b)
(cond
((or(null a)(null b) nil))
((< a (car b)) (list< a (cdr b)))
(t(cons (car b) (list< a (cdr b))))))
(defun list>= (a b)
(cond
((or(null a)(null b) nil))
((>= a (car b)) (list> a (cdr b)))
(t(cons (car b) (list> a (cdr b))))))
我的问题是,当列表<和列表> =完成列表总是以.T结束。例如:
> (list< '4 '(1 5 3 8 2))
Entering: LIST<, Argument list: (4 (1 5 3 8 2))
Entering: LIST<, Argument list: (4 (5 3 8 2))
Entering: LIST<, Argument list: (4 (3 8 2))
Entering: LIST<, Argument list: (4 (8 2))
Entering: LIST<, Argument list: (4 (2))
Entering: LIST<, Argument list: (4 NIL)
Exiting: LIST<, Value: T
Exiting: LIST<, Value: (2 . T)
Exiting: LIST<, Value: (2 . T)
Exiting: LIST<, Value: (3 2 . T)
Exiting: LIST<, Value: (3 2 . T)
Exiting: LIST<, Value: (1 3 2 . T)
(1 3 2 . T)
为什么(4 NIL)评估为T?
我一直指出[从_The Pitmanual_羊诡计(http://www.maclisp.info/pitmanual /funnies.html#sheep_trick)在Common Lisp中进行快速排序时出现。 –
Downvoted for not formatting the source code。 –