;; 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 ((lib "image.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "2htdp"))))) ; 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)] [(cons? lon) (cond [(<= n (first lon)) (cons n lon)] [else (cons (first lon) (insert n (rest lon)))] )]) ) (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: