;; 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-reader.ss" "lang")((modname lec9) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;; A list-of-number is either ;; - empty ;; - (cons num list-of-number) ;; sort : list-of-number -> list-of-number ;; To sort the numbers in lon, returning a list ;; of the same numbers, but in ascending order (define (sort lon) (cond [(empty? lon) empty] [(cons? lon) (insert (first lon) (sort (rest lon)))])) ;; insert : num list-of-number[sorted] -> list-of-number[sorted] ;; To insert n into the right place in a sorted (ascending) list ;; to get a list with n and everything already in lon (define (insert n lon) (cond [(empty? lon) (cons n empty)] [(cons? lon) (cond [(< n (first lon)) (cons n lon)] [(>= n (first lon)) (cons (first lon) (insert n (rest lon)))])])) (check-expect (insert 5 empty) (cons 5 empty)) (check-expect (insert 5 (cons 6 empty)) (cons 5 (cons 6 empty))) (check-expect (insert 5 (cons 2 empty)) (cons 2 (cons 5 empty))) (check-expect (insert 5 (list 0 2 2 3 5 6 7 8)) (list 0 2 2 3 5 5 6 7 8)) (check-expect (sort empty) empty) (check-expect (sort (cons 7 (cons 2 empty))) (cons 2 (cons 7 empty))) (check-expect (sort (list 2 7)) (list 2 7)) (check-expect (sort (list 10 5 7 5 2 12 1)) (list 1 2 5 5 7 10 12)) ;; ---------------------------------------- ;; append-lists : list-of-num list-of-num -> list-of-num ;; To append the two lists ;(define (append-lists lon1 lon2) ; (cond ; [(empty? lon1) ... lon2 ... ] ; [(cons? lon1) ... lon2 ... (first lon1) ... (append-lists (rest lon1) lon2) ...])) (define (append-lists lon1 lon2) (cond [(empty? lon1) lon2] [(cons? lon1) (cons (first lon1) (append-lists (rest lon1) lon2))])) (check-expect (append-lists empty empty) empty) (check-expect (append-lists empty (cons 2 empty)) (cons 2 empty)) (check-expect (append-lists (cons 2 empty) empty) (cons 2 empty)) (check-expect (append-lists (cons 2 empty) (cons 3 empty)) (cons 2 (cons 3 empty))) (check-expect (append-lists (list 2 4 6 8) (list 1 3 5 7)) (list 2 4 6 8 1 3 5 7)) ;; ---------------------------------------- ;; parallel-sum : list-of-num list-of-num[same length] -> list-of-num[same length] ;; To add up numbers in lon1 and lon2 pairwise ;(define (parallel-sum lon1 lon2) ; (cond ; [(empty? lon1) ... ...] ; [(cons? lon1) ... (first lon1) ... ; (first lon2) ... (parallel-sum (rest lon1) (rest lon2)) ...])) (define (parallel-sum lon1 lon2) (cond [(empty? lon1) empty] [(cons? lon1) (cons (+ (first lon1) (first lon2)) (parallel-sum (rest lon1) (rest lon2)))])) (check-expect (parallel-sum empty empty) empty) (check-expect (parallel-sum (cons 3 empty) (cons 2 empty)) (cons 5 empty)) (check-expect (parallel-sum (list 2 4 6 8) (list 1 3 5 7)) (list 3 7 11 15)) ;; ---------------------------------------- ;; merge-lists : list-of-num[sorted] list-of-num[sorted] -> list-of-num[sorted] ;; Combined the two lists to produce a still-sorted combination ;(define (merge-lists lon1 lon2) ; (cond ; [(and (empty? lon1) (empty? lon2)) ...] ; [(and (empty? lon1) (cons? lon2)) ... ; (first lon2) ... ... (merge-lists empty (rest lon2)) ...] ; [(and (cons? lon1) (empty? lon2)) ; (first lon1) ... ... (merge-lists (rest lon1) empty) ...] ; [(and (cons? lon1) (cons? lon2)) ; (first lon1) ... (first lon2) .... (merge-lists (rest lon1) lon2) ; ... (merge-lists lon1 (rest lon2)) ... (merge-lists (rest lon1) (rest lon2)) ....])) (define (merge-lists lon1 lon2) (cond [(and (empty? lon1) (empty? lon2)) empty] [(and (empty? lon1) (cons? lon2)) lon2] [(and (cons? lon1) (empty? lon2)) lon1] [(and (cons? lon1) (cons? lon2)) (cond [(< (first lon1) (first lon2)) (cons (first lon1) (merge-lists (rest lon1) lon2))] [else (cons (first lon2) (merge-lists lon1 (rest lon2)))])])) (check-expect (merge-lists empty empty) empty) (check-expect (merge-lists empty (cons 2 empty)) (cons 2 empty)) (check-expect (merge-lists (cons 2 empty) empty) (cons 2 empty)) (check-expect (merge-lists (cons 2 empty) (cons 3 empty)) (cons 2 (cons 3 empty))) (check-expect (merge-lists (cons 3 empty) (cons 2 empty)) (cons 2 (cons 3 empty))) (check-expect (merge-lists (list 2 4 5 6 8) (list 1 3 5 7)) (list 1 2 3 4 5 5 6 7 8))