[plt-scheme] How to make the following code more clear?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon Oct 13 14:44:24 EDT 2008

#lang typed-scheme

(: selectionsort (∀ (α) ((Listof α) (α α -> Boolean) -> (Listof  
α))))
;; gen. rec.: repeatedly pick/remove the largest value wrt <=, create  
lists from it
(define (selectionsort l0 <=)
   (: max (α α -> α))
   (define (max n m) (if (<= n m) m n))

   (: = (α α -> Boolean))
   (define (= n m) (and (<= n m) (<= m n)))

   (: largest ((cons α (Listof α)) -> α))
   ;; pick the largest value from the list
   (define (largest lst)
     (foldr max (car lst) (cdr lst)))

   (: remove ((Listof α) α -> (Listof α)))
   ;; remove the given value from the list
   (define (remove lst val)
     (cond
       [(= (car lst) val) (cdr lst)]
       [else (cons (car lst) (remove (cdr lst) val))]))

   (: aux ((Listof α) -> (Listof α)))
   (define (aux l)
     (cond
       [(null? l) '()]
       [else (let ([m (largest l)]) (cons m (aux (remove l m))))]))

   (aux l0))

(equal? (selectionsort '(2 1 4 3) <=) '(4 3 2 1))
(equal? (selectionsort '(2 1 4 3) >=) '(1 2 3 4))

Posted on the users mailing list.