;; 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-intermediate-lambda-reader.ss" "lang")((modname insert) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
;; insert: list-of-number -> list-of-number (in ascending order)
;; purpose: given a ascending (technically non-descending) list of numbers lon, (insert k lon) returns an ascending list containing the numbers in lon and the number k.
;; Examples:
(check-expect (insert 0 empty) '(0))
(check-expect (insert -1 '(0 2)) '(-1 0 2))
(check-expect (insert 3 '(0 2)) '(0 2 3))
(check-expect (insert 1 '(0 2)) '(0 1 2))
(check-expect (insert 6 '(-4 5 17 20)) '(-4 5 6 17 20))
;; Template: natural recursion on lon
;; (define (insert k lon)
;; (cond ([empty? lon) ... k ...)
;; (else ... (first lon) ... (insert k (rest lon)) ... )
;; Code:
(define (insert k lon)
(cond ([empty? lon] (list k))
([<= k (first lon)] (cons k lon))
(else (cons (first lon) (insert k (rest lon))))))