; A list-of-num is ; - empty ; - (cons num list-of-num) ;(define (func-for-lon l) ; (cond ; [(empty? l) ...] ; [(cons? l) ; ... (first l) ; ... (func-for-lon (rest l)) ...])) ; sort-list : list-of-num -> list-of-num (define (sort-list l) (cond [(empty? l) empty] [(cons? l) (insert (first l) (sort-list (rest l)))])) ; insert : num list-of-num -> list-of-num ; Puts n into the sorted list l to poroduce ; a sorted list (define (insert n l) (cond [(empty? l) (list n)] [(cons? l) (cond [(< n (first l)) (cons n l)] [else (cons (first l) (insert n (rest l)))])])) (insert 10 empty) "should be" (list 10) (insert 5 (list 2 3 7)) "should be" (list 2 3 5 7) (insert 0 (list 2 3 7)) "should be" (list 0 2 3 7) (sort-list empty) "should be" empty (sort-list (list 1)) "should be" (list 1) (sort-list (list 1 0 3)) "should be" (list 0 1 3) ;(sort-list (list 1 0 3)) ;--> (insert 1 (sort-list (list 0 3))) ;--> (insert 1 (insert 0 (sort-list (list 3)))) ;--> (insert 1 (insert 0 (insert 3 (sort-list empty)))) ;--> (insert 1 (insert 0 (insert 3 empty))) ;--> (insert 1 (insert 0 (list 3))) ;--> (insert 1 (list 0 3)) ;--> (list 0 1 3) ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; append-lists : list-of-num list-of-num -> list-of-num (define (append-lists al bl) (cond [(empty? al) bl] [(cons? al) (cons (first al) (append-lists (rest al) bl))])) (append-lists empty empty) "should be" empty (append-lists (list 1 3 5) (list 0 4 6)) "should be" (list 1 3 5 0 4 6) ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; parallel-sum : list-of-num list-of-num -> list-of-num (define (parallel-sum al bl) (cond [(empty? al) empty] [(cons? al) (cons (+ (first al) (first bl)) (parallel-sum (rest al) (rest bl)))])) (parallel-sum empty empty) "should be" empty (parallel-sum (list 1 3 5) (list 0 4 6)) "should be" (list 1 7 11) ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; merge-lists : list-of-num list-of-num -> list-of-num (define (merge-lists al bl) (cond [(and (empty? al) (empty? bl)) empty] [(and (empty? al) (cons? bl)) bl] [(and (cons? al) (empty? bl)) al] [(and (cons? al) (cons? bl)) (cond [(< (first al) (first bl)) (cons (first al) (merge-lists (rest al) bl))] [else (cons (first bl) (merge-lists al (rest bl)))])])) (merge-lists empty empty) "should be" empty (merge-lists (list 1 3 5) (list 0 4 6)) "should be" (list 0 1 3 4 5 6) ;(merge-lists (list 1 3 5) (list 0 4 6)) ;--> (cons 0 (merge-lists (list 1 3 5) (list 4 6))) ;--> (cons 0 (cons 1 (merge-lists (list 3 5) (list 4 6)))) ;--> (cons 0 (cons 1 (cons 3 (merge-lists (list 5) (list 4 6))))) ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; create-up-list : num -> list-of-num (define (create-up-list n) (create-m-to-n-list 0 n)) ; Creates a list from m to n (define (create-m-to-n-list m n) (cond [(= m n) (list n)] [else (cons m (create-m-to-n-list (add1 m) n))])) (create-up-list 3) "should be" (list 0 1 2 3)