;; 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 csv) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;;DEMO: Parsing comma-separated values (or USGS point-of-interest data) ;;FEATURE_ID|FEATURE_NAME|FEATURE_CLASS|STATE_ALPHA|STATE_NUMERIC|COUNTY_NAME|COUNTY_NUMERIC|PRIMARY_LAT_DMS|PRIM_LONG_DMS|PRIM_LAT_DEC|PRIM_LONG_DEC|SOURCE_LAT_DMS|SOURCE_LONG_DMS|SOURCE_LAT_DEC|SOURCE_LONG_DEC|ELEV_IN_M|ELEV_IN_FT|MAP_NAME|DATE_CREATED|DATE_EDITED ;;399|Agua Sal Creek|Stream|AZ|04|Apache|001|362740N|1092842W|36.4611122|-109.4784394|362053N|1090915W|36.3480582|-109.1542662|1645|5397|Fire Dance Mesa|02/08/1980| ;;400|Agua Sal Wash|Valley|AZ|04|Apache|001|363246N|1093103W|36.546112|-109.5176069|362740N|1092842W|36.4611122|-109.4784394|1597|5239|Little Round Rock|02/08/1980| ;;401|Aguaje Draw|Valley|AZ|04|Apache|001|343417N|1091313W|34.5714281|-109.2203696|344308N|1085826W|34.7188|-108.9739|1750|5741|Kearn Lake|02/08/1980|01/14/2008 ;; a CSV is (listof Datum) ;; a Datum is a Number or 'EOF empty (list 1 2 3 'EOF) (list 1 2 3 'EOF 1 2 'EOF) (list 'EOF 'EOF 1 2 3 'EOF) ;; csv->table: CSV -> Table ;; csv->table: transforms the given csv to a table ;; (csv->table empty) ==> empty ;; (csv->table (list 1 2 3 'EOF)) ==> (list (list 1 2 3)) ;; (csv->table (list 1 2 3 'EOF 1 2 'EOF)) ==> (list (list 1 2 3) (list 1 2)) ;; (csv->table (list 'EOF 'EOF 1 2 3 'EOF)) ==> (list empty empty (list 1 2 3)) ;; structural template #; (define (csv->table a-csv) (cond [(empty? a-csv) (... a-csv ...)] [else (... a-csv ... ... (first a-csv) ... ... (rest a-csv) ... ... (csv->table (rest a-csv)) ...)])) ;; generative template #; (define (csv->table a-csv) (cond [(trivially-solvable? a-csv) (determine-solution a-csv)] [else (combine-solutions ... a-csv ... (csv->table (generate-problem a-csv)))])) (define (csv->table a-csv) (cond [(empty? a-csv) a-csv] [else (cons (get-first-line a-csv) (csv->table (remove-first-line a-csv)))])) ;; termination argument: the recursive call to csv->table consumes a csv with less "lines" (check-expect (csv->table empty) empty) (check-expect (csv->table (list 1 2 3 'EOF)) (list (list 1 2 3))) (check-expect (csv->table (list 1 2 3 'EOF 1 2 'EOF)) (list (list 1 2 3) (list 1 2))) (check-expect (csv->table (list 'EOF 'EOF 1 2 3 'EOF)) (list empty empty (list 1 2 3))) ;; get-first-line: CSV -> Line ;; get-first-line: returns the first line of the given CSV ;; (get-first-line empty) ==> empty ;; (get-first-line (list 1 2 3 'EOF)) ==> (list 1 2 3) ;; (get-first-line (list 1 2 3 'EOF 1 2 'EOF)) ==> (list 1 2 3) ;; (get-first-line (list 'EOF 'EOF 1 2 3 'EOF)) ==> empty #; (define (get-first-line a-csv) (cond [(empty? a-csv) (... a-csv ...)] [else (... a-csv ... ... (first a-csv) ... ... (rest a-csv) .... ... (get-first-line (rest a-csv)) ...)])) (define (get-first-line a-csv) (cond [(empty? a-csv) a-csv] [else (cond [(symbol? (first a-csv)) empty] [else (cons (first a-csv) (get-first-line (rest a-csv)))])])) (check-expect (get-first-line empty) empty) (check-expect (get-first-line (list 1 2 3 'EOF)) (list 1 2 3)) (check-expect (get-first-line (list 1 2 3 'EOF 1 2 'EOF)) (list 1 2 3)) (check-expect (get-first-line (list 'EOF 'EOF 1 2 3 'EOF)) empty) ;; remove-first-line: CSV -> CSV ;; remove-first-line: returns the given CSV without the first line ;; (remove-first-line empty) ==> empty ;; (remove-first-line (list 1 2 3 'EOF)) ==> empty ;; (remove-first-line (list 1 2 3 'EOF 1 2 'EOF)) ==> (list 1 2 'EOF) ;; (remove-first-line (list 'EOF 'EOF 1 2 3 'EOF)) ==> (list 'EOF 'EOF 1 2 3 'EOF)) #; (define (remove-first-line a-csv) (cond [(empty? a-csv) (... a-csv ...)] [else (... a-csv ... ... (first a-csv) ... ... (rest a-csv) .... ... (remove-first-line (rest a-csv)) ...)])) (define (remove-first-line a-csv) (cond [(empty? a-csv) a-csv] [else (cond [(symbol? (first a-csv)) (rest a-csv)] [else (remove-first-line (rest a-csv))])])) (check-expect (remove-first-line empty) empty) (check-expect (remove-first-line (list 1 2 3 'EOF)) empty) (check-expect (remove-first-line (list 1 2 3 'EOF 1 2 'EOF)) (list 1 2 'EOF)) (check-expect (remove-first-line (list 'EOF 'EOF 1 2 3 'EOF)) (list 'EOF 1 2 3 'EOF))