About me

About me

Feeds

RSS feed

Shortest sort

19th April 2018

Here's a challenge I was thinking about recently: write the shortest Common Lisp function to sort a list of numbers into ascending order. For example:

CL-USER > (mysort '(3 1 4 1 5 9 2 6 5 3 5 8))
(1 1 2 3 3 4 5 5 5 6 8 9)

You can use any Common Lisp functions you want, apart, of course, from sort or stable-sort.

To get your score, count the non-nil atoms in your function definition(s) using count-atoms:

(defun count-atoms (form)
  (typecase form
    (null 0)
    (atom 1)
    (cons (+ (count-atoms (car form))
             (count-atoms (cdr form))))))

Post your answer in the comments. If you can get below about 40 you're doing OK! I'll give my answer in a week's time.


blog comments powered by Disqus