William James
11/1/2015 7:04:00 PM
WJ wrote:
> > It is buggy. Sorry. Here is a better version
> >
> > (defun qsort (list &aux (x (first list)) (xs (rest list)))
> > (when list
> > (append (qsort (remove-if (lambda (y) (>= y x)) xs))
> > (list x)
> > (qsort (remove-if (lambda (y) (< y x)) xs)))))
>
> Gauche Scheme:
>
> (define (qsort xs)
> (if (null? xs)
> xs
> (receive (lt gt) (partition (pa$ > (car xs)) (cdr xs))
> `(,@(qsort lt) ,(car xs) ,@(qsort gt)))))
>
>
> gosh> (qsort '(9 2 0 88 33 22 1983 1066 3))
> (0 2 3 9 22 33 88 1066 1983)
It's shorter in MatzLisp (Ruby):
def qsort((f,*r))
f ? (l,g=r.partition{|x| f>x}; qsort(l)+[f]+qsort(g)) : []
end
Explanation:
def qsort((f,*r))
The array passed to the function is destructured; f receives
the first element, and r receives the rest.
f ? (l,g=r.partition{|x| f>x}; qsort(l)+[f]+qsort(g)) : []
The
<condition> ? <do-if-true> : <do-if-false>
construct is borrowed from C.
If f is not nil (the array isn't empty), then l is
bound to the numbers less than f, and g receives the numbers
greater than or equal to f (the first number in the array).
The rest should be easy to understand.
qsort [9,2,0,88,33,22,1983,1066,3]
==>[0, 2, 3, 9, 22, 33, 88, 1066, 1983]
--
It is extremely gratifying to ... achieve membership in an elite that is going
to make history.... The only drawback is that the self-appointed shepherds are
likely to find eventually that they were only sheepdogs and herded the flock
toward a destination of which they knew nothing. --- R. P. Oliver