You are trying to return the -1th element of a list which won't work. nth returns the nth element of the list so (nth 0 '(1 2 3)) will return 1. But at some point in your code it calls (nth -1 (-5 -80 -4 -1 0 0 ...)) and boom!
Other notes about your code:
- Putting the closing ) on a seperate line is not good lisp style and makes your code harder to read. Lisp code is made of lists, the parens are not really like the curly braces of other languages.
- Use an editor that supports your language. I recommend Vim with Slim or Emacs with slime. If you use emacs with slime this video may help you get started.
- Dont use camel casing in your names. All symbols are upcased in common lisp when they are interned so 'HelloThere is EXACTLY the same symbol as 'hellothere or 'HELLOTHERE
Also please look at the rosettacode page for quicksort to see how to do it in common-lisp (and lots of other languages).
;; Here is a functional version for example.
(defun quicksort (list &aux (pivot (car list)) )
(if (rest list)
(concatenate 'list
(quicksort (remove-if-not #'(lambda (x) (< x pivot)) list))
(remove-if-not #'(lambda (x) (= x pivot)) list)
(quicksort (remove-if-not #'(lambda (x) (> x pivot)) list)))
list))
If you are not used to reading lambdas then here is the same code as above but with the lambdas made into local functions so the code reads a little more like english.
(defun quicksort (list &aux (pivot (car list)))
(labels ((less-than-pivot (x) (< x pivot))
(greater-than-pivot (x) (> x pivot))
(equal-to-pivot (x) (= x pivot)))
(if (rest list)
(concatenate 'list
(quicksort (remove-if-not #'less-than-pivot list))
(remove-if-not #'equal-to-pivot list)
(quicksort (remove-if-not #'greater-than-pivot list)))
list)))
The second version is a little less tidy in my mind. In both of these examples you can see how the recursive approach lets you think about how to do just one step and then by calling itself you apply the solution for one step to get the solution to the entire problem