我正在嘗試使用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。 –