1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname |27.2|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
4 ;Exercise 27.2.1. Determine what the list-of-lines representation for empty, (list 'NL), and (list 'NL 'NL) should be. Why are these examples important test cases?
9 ;where s is a symbol and f is a file.
13 ;(file->list-of-lines empty)
15 ;(file->list-of-lines (list 'NL))
17 ;(file->list-of-lines (list 'NL 'NL))
20 ;The NEWLINE character is represented by 'NL
23 ;file->list-of-lines : file -> (listof (listof symbols))
24 ;Converts a-file into a (listof (listof symbols)) representing the file. Each new list starts right at the end of a newline symbol and ends at the first occurrence of a newline symbol. file->list-of-lines uses generative recursion: the first line is appended to the front of the remainder of the file that has already been converted into a list of lines.
25 ;Termination Argument: Since each application of file->list-of-lines necessarily shortens the argument for the new generative recursion, the paramater must get smaller over time and eventually become the empty list, for which a trivial solution is available.
27 ;first-line : file -> (listof symbols)
28 ;Returns the first line of a-file.
30 ;remove-first-line : file -> file
31 ;Given a-file, remove the first line.
33 (define (file->list-of-lines a-file)
34 (local ((define (first-line a-file)
36 [(symbol=? NEWLINE (first a-file)) empty]
37 [else (cons (first a-file)
38 (first-line (rest a-file)))]))
39 (define (remove-first-line a-file)
41 [(symbol=? NEWLINE (first a-file)) (rest a-file)]
42 [else (remove-first-line (rest a-file))])))
44 [(empty? a-file) empty]
45 [else (cons (first-line a-file)
46 (file->list-of-lines (remove-first-line a-file)))])))
49 ;(file->list-of-lines '(Hi my name is Joe NL
50 ; and I live in a button factory NL
54 ;A file of numbers (fon) is either
58 ;where N is a number and F is a file.
60 (define-struct rr (table costs))
62 ;A restaurant is a structure
64 ;where ta is a number and co is a (listof numbers).
66 ;file->list-of-checks : fon -> (listof rr)
67 ;Given afon, file->list-of-checks converts afon into a (listof rr). The first number in each line in afon represents the table number and the remaining numbers up to the NEWLINE represent the costs per table. We again use generative recursion. First, we create an rr structure with the first table and its associated costs, and then we append that to the beginning of an already-created (listof rr) of the remaining file-of-numbers.
68 ;Termination Argument: each file that is passed into file->list-of-checks must necessary get smaller and ultimately reach empty since one line is removed every recursion.
72 ;(equal? (file->list-of-checks
73 ; (list 1 2.30 4.00 12.50 13.50 'NL
76 ; (list (make-rr 1 (list 2.30 4.00 12.50 13.50))
77 ; (make-rr 2 (list 4.00 18.00))
78 ; (make-rr 4 (list 2.30 12.50))))
80 (define (file->list-of-checks afon)
83 [(and (symbol? (first afon))
84 (symbol=? NEWLINE (first afon))) (file->list-of-checks (remove-first-line afon))]
85 [else (cons (make-rr (first-table afon)
87 (file->list-of-checks (remove-first-line afon)))]))
89 (define (remove-first-line a-file)
91 [(empty? a-file) empty]
92 [(and (symbol? (first a-file))
93 (symbol=? NEWLINE (first a-file))) (rest a-file)]
94 [else (remove-first-line (rest a-file))]))
96 ;first-table : fon -> number
97 ;Given afon, return the table associated with it (if (first afon) is a number, it is simply the first number--if (first afon) is empty or NEWLINE, it is empty).
99 (define (first-table afon)
101 [(number? (first afon)) (first afon)]
104 ;first-costs : fon -> (listof numbers)
105 ;Given afon, return the first line of (listof numbers) after the table number.
107 (define (first-costs afon)
108 (first-line (rest afon)))
110 ;first-line : (listof X) -> (listof X)
111 ;Returns the first line of alox.
113 (define (first-line a-file)
115 [(empty? a-file) empty]
116 [(and (symbol? (first a-file))
117 (symbol=? NEWLINE (first a-file))) empty]
118 [else (cons (first a-file)
119 (first-line (rest a-file)))]))
122 ;(file->list-of-checks (list 1 2.5 3.5 4.5 'NL
124 ; 3 8.5 2.4 3.9 'NL))
126 ;(equal? (file->list-of-checks
127 ; (list 1 2.30 4.00 12.50 13.50 'NL
130 ; (list (make-rr 1 (list 2.30 4.00 12.50 13.50))
131 ; (make-rr 2 (list 4.00 18.00))
132 ; (make-rr 4 (list 2.30 12.50))))