[plt-scheme] Some fine distinctions

From: Arthur Nunes-Harwit (anh at cs.rit.edu)
Date: Wed May 13 10:37:50 EDT 2009

Hi,

On Tue, 12 May 2009, Matthias Felleisen wrote:

>> Tail recursion vs Accumulative recursion.
>
> A function that uses an accumulator doesn't have to be in tail-recursive 
> shape.

Here's another example (in Common LISP): an implementation of quicksort 
that does not use append.

(defun partition (pivot L &optional small large)
   (cond ((null L)
          (values small large))
         ((<= (first L) pivot)
          (partition pivot (rest L) (cons (first L) small) large))
         (t (partition pivot (rest L) small (cons (first L) large)))))

(defun quicksortam (L &optional a)
   (if (null L)
       a
     (let ((pivot (first L)))
       (multiple-value-bind (less-or-same more)
           (partition pivot (rest L))
         (quicksortam less-or-same
                      (cons pivot (quicksortam more a)))))))


-Arthur


Posted on the users mailing list.