;; 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 tables) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;; DEMO: Lines and Tables ;; A Table is a (listof Line) ;; A Line is a (listof Number) empty (list empty) (list (list 1 2 3) (list 1 2 3)) (list (list 1 2 4) (list 2 4 5 6)) ;; table-summary: Table -> Line ;; table-summary: aggregates the columns of the given table as a line ;; (table-summary (list empty)) ==> empty ;; (table-summary (list (list empty))) => empty ;; (table-summary (list (list 1 2 3) (list 1 2 3))) => (list 2 4 6) ;; (table-summary (list (list 1 2 4) (list 2 4 5 6))) => (list 3 6 9 6) #;(define (table-summary a-table) (cond [(empty? a-table) (... a-table ...)] [else ( ... a-table ... ... (first a-table) ... ... (rest a-table) ... ... (table-summary (rest a-table)) ...)])) (define (table-summary a-table) (cond [(empty? a-table) empty] [else (add-lines (first a-table) (table-summary (rest a-table)))])) #;(define (table-summary a-table) (foldr add-lines empty a-table)) (check-expect (table-summary empty) empty) (check-expect (table-summary (list empty)) empty) (check-expect (table-summary (list (list 1 2 3) (list 1 2 3))) (list 2 4 6)) (check-expect (table-summary (list (list 1 2 4) (list 2 4 5 6))) (list 3 6 9 6)) ;; add-lines: Line Line -> Line ;; add-lines: aggregates the two given lines in a single line ;; (add-lines empty empty) ==> empty ;; (add-lines empty (list 1 2 3)) => (list 1 2 3) ;; (add-lines (list 1 2 3) empty) => (list 1 2 3) ;; (add-lines (list 1 2 4) (list 2 4 5)) => (list 3 6 9) ;; (add-lines (list 1 2 4 10) (list 2 4 5)) => (list 3 6 9 10) ;; (add-lines (list 1 2 4) (list 2 4 5 10)) => (list 3 6 9 10) #;(define (add-lines a-line b-line) (cond [(and (empty? a-line) (empty? b-line)) (... a-line ... b-line ...)] [(and (empty? a-line) (cons? b-line)) (... a-line ... ... b-line ... ... (first b-line) ... ... (rest b-line) ...)] [(and (empty? b-line) (cons? a-line)) (... b-line ... ... a-line ... ... (first a-line) ... ... (rest a-line) ...)] [else (... a-line ... ... (first a-line) ... ... (rest a-line) ... ... b-line ... ... (first b-line) ... ... (rest b-line) ... (add-lines (rest a-line) (rest b-line)) ...)])) (define (add-lines a-line b-line) (cond [(and (empty? a-line) (empty? b-line)) empty] [(empty? a-line) b-line] [(empty? b-line) a-line] [else (cons (+ (first a-line) (first b-line)) (add-lines (rest a-line) (rest b-line)))])) (check-expect (add-lines empty empty) empty) (check-expect (add-lines empty (list 1 2 3)) (list 1 2 3)) (check-expect (add-lines (list 1 2 3) empty) (list 1 2 3)) (check-expect (add-lines (list 1 2 4) (list 2 4 )) (list 3 6 4)) (check-expect (add-lines (list 1 2 4 10) (list 2 4 5)) (list 3 6 9 10)) (check-expect (add-lines (list 1 2 4) (list 2 4 5 10)) (list 3 6 9 10))