; Continuation Passing Sytle (CPS) Quicksort algorithm (define cps-qsort (lambda (pivot ls k) ;; k is the "continuation" procedure (if (null? ls) (k '() '()) (cps-qsort pivot (cdr ls) ;; tail call (lambda (left right) (let ((x (car ls))) (if (< x pivot) (k (cons x left) right) (k left (cons x right))))))))) ; quicksort "driver" procedure that creates the ; continuation to send to "cps-qsort" (define qsort (lambda (ls) (if (null? ls) '() (let ((pivot (car ls))) (cps-qsort pivot (cdr ls) (lambda (left right) ; "continuation" function (append (qsort left) (list pivot) (qsort right))) ))))) (qsort '()) (qsort '(0)) (qsort '(1 2 3 4 5)) (qsort '(5 4 3 2 1)) (qsort '(0 -1 1 -2 2 -3 3 -4 4 -5 5)) (qsort '(-99 0 1024 202 -1 77 18 -34 -33 2048 19 1 9 7 2 3 -3 191))