#|
function is named quick-sort since quicksort is built in.
Note: need to be in intermediate language level to use local
|#

; quick-sort : list-of-number ->list-of-number
; sorts list into increasing order
(define (quick-sort alon)
  (cond [(empty? alon) empty] 
        [(cons? alon)
         (local [(define pivot (first alon))]
           (append (quick-sort (smaller-than pivot (rest alon)))
                   (list pivot)
                   (quick-sort (larger-than pivot (rest alon)))))]))

; smaller-than : number list-of-number -> list-of-number
; produces list of nums from original list that are smaller than given number
(define (smaller-than anum alon)
  (cond [(empty? alon) empty]
        [(cons? alon) (cond [(< (first alon) anum) 
                             (cons (first alon) (smaller-than anum (rest alon)))]
                            [else (smaller-than anum (rest alon))])]))

; larger-than : number list-of-number -> list-of-number
; produces list of nums from original list that are larger than given number
(define (larger-than anum alon)
  (cond [(empty? alon) empty]
        [(cons? alon) (cond [(> (first alon) anum) 
                             (cons (first alon) (larger-than anum (rest alon)))]
                            [else (larger-than anum (rest alon))])]))