CSC 270: Survey of Programming Languages

Scheme (Racket) Lecture 10 - An Extended Example: Sorting

An Extended Example: Sorting

Starting the Sort Program

Examples of Sorts

Templates for the Sort Program

The Simple Case for the Sort Program

Developing the non-empty case

Writing insert

The final sort program

;; sort : list-of-numbers -> list-of-numbers
;; to create a list of numbers with the same
;; numbers
;; as alon sorted in descending order
(define (sort alon)
  (cond
    [(empty? alon) empty]
    [(cons? alon) (insert (first alon)
                        (sort (rest alon)))]))

The final insert program

;; insert : number sorted-number-list -> number-list
;; to create a list of numbers from n and alon that is
;; sorted in descending order; alon is sorted
(define (insert n alon)
  (cond
    [(empty? alon) (cons n empty)]
    [else (cond
            [(> n (first alon)) (cons n alon)]
            [else (cons (first alon)
    (insert n (rest alon)))])]))
 

[Back to the Notes Index]