;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname week3_w_full) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; a list-of-numbers is either: ; - empty ; - (cons number list-of-numbers) ; template: ; (define (fun-for-list-of-numbers numlist) ; (cond [(empty? numlist) ...] ; [(cons? numlist) ... (first numlist) ... (fun-for-list-of-numbers (rest numlist)) ... ]) ; sort : list-of-numbers -> list-of-numbers ; to sort a list of numbers in ascending order (define (sort lon) (cond [(empty? lon) empty] [(cons? lon) (insert (first lon) (sort (rest lon)))]) ) (check-expect (sort empty) empty) (check-expect (sort (cons 1 (cons 2 empty))) (cons 1 (cons 2 empty))) (check-expect (sort (cons 2 (cons 1 empty))) (cons 1 (cons 2 empty))) ; insert : number list-of-numbers -> list-of-numbers ; inserts number into list, assuming list is sorted (define (insert n lon) (cond [(empty? lon) (cons n empty)] [else (cond [(< n (first lon)) (cons n lon)] [(> n (first lon)) (cons (first lon) (insert n (rest lon)))] [(= (first lon) n) (cons n lon)])])) (check-expect (insert 2 empty) (cons 2 empty)) (check-expect (insert 2 (cons 3 empty)) (cons 2 (cons 3 empty))) (check-expect (insert 9 (cons 1 (cons 3 (cons 8 (cons 10 empty))))) (cons 1 (cons 3 (cons 8 (cons 9 (cons 10 empty)))))) ; list shortcuts: (list 1 2 3) (cons 1 (cons 2 (cons 3 empty))) '(1 2 3)