regrading sublist on quick sort
I have trouble with doing quicksort in Lisp.
My objective is:
If a list contains 0 or 1 element, it is already sorted. Otherwise sort it as follows: First get the pivot, which is the first element of the list. Now go through the rest of the list (by using DOLIST), compare each element with the pivot. If an element is less than or equal to the pivot, put it in the left-sublist, otherwise put it in the right-sublist (by using CONS or APPEND). Then call Qsort to sort left-sublist and right-sublist. Finally, combine left-sublist, pivot, and right-sublist into one sorted list.
This is my code:
(defun qsort (x)
(if (>= 1 (length x))
(format t"~%~s is already sorted" x)
((let (pivot (first x))
(dolist (i x )
(if (>= pivot (nth i x))
(list y (nth i))
(li开发者_开发技巧st z (nth i))))
(appned y z)))))
yet I seem to hit the wall by getting
Badly formed lambda: (LET (PIVOT (FIRST X)) (DOLIST (I X PIVOT) (IF (>= PIVOT (NTH I X)) (LIST PIVOT (NTH I)) (LIST PIVOT (NTH I)))))
from the compiler.
I hope you can help. Thank you.
Why are there two layers of parentheses around LET?
Why is there a layer of parentheses missing inside LET around the bindings?
Why do you call (nth i x) ? Why with i?
Why do you call NTH with one argument, when it takes two?
What function is APPNED? There is no function of that name.
(>= x 1) is (plusp x)
and more...
精彩评论