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

From: SamuelXiao (foolsmart2005 at gmail.com)
Date: Tue Oct 14 05:57:58 EDT 2008

Thanks very much.

On Oct 14, 2:44 am, Matthias Felleisen <matth... at ccs.neu.edu> wrote:
> #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))_________________________________________________
>   For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme


Posted on the users mailing list.