;;; An Example of CPS
(define (qsort-cps lst)
(define (cps lst k)
(cond [(empty? lst)
(k lst)]
[else
(define head (first lst))
(define-values (lolst hilst)
(partition (lambda (x) (< x head)) (rest lst)))
(cps lolst
(lambda (sorted-lolst)
(cps hilst
(lambda (sorted-hilst)
(k (append sorted-lolst
(list head)
sorted-hilst))))))]))
(cps lst identity))
;;; An Example of Monad
(struct number/logs (num logs) #:transparent)
(define (apply/logs x f)
(define y (f (number/logs-num x)))
(number/logs (number/logs-num y)
(append (number/logs-logs x)
(number/logs-logs y))))
(define (square/logs x)
(define y (* x x))
(number/logs
(* x x)
(list (format "Squared ~a to get ~a." x y))))
(define (add1/logs x)
(define y (add1 x))
(number/logs
y
(list (format "Added 1 to ~a to get ~a." x y))))
(define (make-number/logs x)
(number/logs x null))
(define a (make-number/logs 5))
(define b (apply/logs a add1/logs))
(define c (apply/logs b square/logs))