HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

Lisp quicksort code

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
codelispquicksort

Problem

I would really appreciate it if someone could review my quicksort implementation. Additionally, I generated my list dynamically and wrote a couple of tests. Of course, I realize that the tests are not complete, but I decided to stop where I was and see if I could get some feedback.

```
(defun categorize_list(pivot thelist less more)
(let ((comparee (car thelist)) (therest (cdr thelist)))
(if (null thelist)
(list less more)
(if (< comparee pivot)
(categorize_list pivot therest (append less (list comparee)) more)
(categorize_list pivot therest less (append more (list comparee)))
)
)
)
)

(defun quicksort(thelist)
(if (null thelist)
()
(let (
(pivot (car thelist))
(therest (cdr thelist))
)
(let ((categorized_list (categorize_list pivot therest () ())))

(append (quicksort (nth 0 categorized_list)) (list pivot) (quicksort (nth 1 categorized_list)))
)
)
)
)

(defun make_list(thelist remaininglength)
(if (eq remaininglength 0)
thelist
(make_list (append (list (random 25)) thelist) (- remaininglength 1))
)
)

(defun should_be_least_to_greatest(thelist)
(if (< (length thelist) 2)
nil
(if (<= (nth 0 thelist) (nth 1 thelist))
(should_be_least_to_greatest (cdr thelist))
(error "Out of order: ~d !< ~d ~%" (nth 0 thelist) (nth 1 thelist))
)
)
)

(defun test_should_become_in_order(thelist)
(let ((sortedList (quicksort thelist)))
(format t "IN: ~a ~% SD: ~a ~% Testing sort.~%" thelist sortedList)
(should_be_least_to_greatest sortedList)
)
)

(defun test_should_maintain_length(thelist)
(if (not (eql (length thelist) (length (quicksort thelist))))
(error "The sorted list is a different length than the original list! ~%")
)
)

(let ((thelist (make_list () 10)))
(test_should_become_in_order thelist)
(test_should_maintain_lengt

Solution

Your formatting is weird. It's going to be a lot easier for other programmers to read your code if you use a more standard style. For example, you have lots of lines with nothing but a hanging parenthesis. Your functions have no docstrings. Sometimes you use_underscores and sometimes you use compoundwords but the Lisp style is hypenated-words. Your indentation is inconsistent: learn the command in your text editor to indent your source code for you, and use it.

Instead of:

(if (not (eql (length thelist) (length (quicksort thelist))))


Using EQL for numbers seems odd. I think = would be preferred. An IF with only one branch is strange, too: WHEN or UNLESS tends to be preferred. Thus:

(unless (= (length thelist) (length (quicksort thelist)))


In another place, you do a comparison with:

(eq remaininglength 0)


The behavior of EQ with integers is undefined. You can use = or in this case:

(zerop remaininglength)


You're using recursion a lot even when it's not needed, e.g., in make_list and should_be_least_to_greatest. Common Lisp doesn't require tail-call optimization, and it's not generally common style. LOOP (or ITERATE) would probably be simpler and more efficient here.

You walk a list applying <=. Lisp's <= already takes any number of arguments, so if you don't need to know the specific elements which are out of order, you can just apply it once.

Code Snippets

(if (not (eql (length thelist) (length (quicksort thelist))))
(unless (= (length thelist) (length (quicksort thelist)))
(eq remaininglength 0)
(zerop remaininglength)

Context

StackExchange Code Review Q#1114, answer score: 8

Revisions (0)

No revisions yet.