Friday, March 12, 2010

Lisp Insertion Sort: Beautiful!

Table of Contents

1 Insertion Sort in Lisp

CL-USER>(defun i-sort (lon)
          (cond ((null lon) nil)
                (t (ordered-insert (first lon) (i-sort (rest lon))))))
I-SORT
CL-USER> (defun ordered-insert (num lon)
           (cond ((null lon) (cons num nil))
                 (t (cond ((>= num (first lon)) (cons num lon))
                          (t (cons (first lon) 
                                   (ordered-insert num (rest lon))))))))
ORDERED-INSERT
CL-USER> (i-sort '(4 1 2 3 5))
(5 4 3 2 1)
CL-USER>

Ahh!! Beautiful.

No comments:

Post a Comment