While we are at it

(define qs
   (lambda (t)
	(if (null? t)
          t
          (let ((head (car t))
                (tail (cdr t)))
             (append (qs (filter (lambda (x) (<= x head)) tail))
                     (list head)
                             (qs (filter (lambda (x) (> x head)) tail)))))))

A non optimized quick-sort in the same vein. Good for toying with Bigloo’s trace system.

(define insert
   (lambda (elem t)
      (if (null? t)
          (list elem)
          (let ((head (car t))
                (tail (cdr t)))
             (if (<= elem head)
                 (append (list elem) t)
                 (append (list head) (insert elem (cdr t))))))))

(define tri-insertion
   (lambda (t)
      (if (null? t)
          t
          (insert (car t) (tri-insertion (cdr t))))))

An insertion sort. Worked on the first try, yeah \o/ .

Post a Comment

Your email is never published nor shared. Required fields are marked *