Blob


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?
6 ;A file is either
7 ;1. empty or
8 ;2. (cons s f)
9 ;where s is a symbol and f is a file.
11 ;Examples
12 ;
13 ;(file->list-of-lines empty)
14 ;empty
15 ;(file->list-of-lines (list 'NL))
16 ;(list empty)
17 ;(file->list-of-lines (list 'NL 'NL))
18 ;(list empty empty)
20 ;The NEWLINE character is represented by 'NL
21 (define NEWLINE '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)
35 (cond
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)
40 (cond
41 [(symbol=? NEWLINE (first a-file)) (rest a-file)]
42 [else (remove-first-line (rest a-file))])))
43 (cond
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
51 ; I have a wife NL
52 ; and three kids NL))
53 ;
54 ;A file of numbers (fon) is either
55 ;1. empty, or
56 ;2. (cons N F),
57 ;3. (cons 'NL F)
58 ;where N is a number and F is a file.
60 (define-struct rr (table costs))
61 ;
62 ;A restaurant is a structure
63 ;(make-rr ta co)
64 ;where ta is a number and co is a (listof numbers).
65 ;
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.
69 ;
70 ;Examples
71 ;
72 ;(equal? (file->list-of-checks
73 ; (list 1 2.30 4.00 12.50 13.50 'NL
74 ; 2 4.00 18.00 'NL
75 ; 4 2.30 12.50))
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)
81 (cond
82 [(empty? afon) empty]
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)
86 (first-costs afon))
87 (file->list-of-checks (remove-first-line afon)))]))
89 (define (remove-first-line a-file)
90 (cond
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)
100 (cond
101 [(number? (first afon)) (first afon)]
102 [else 'error]))
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)
114 (cond
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)))]))
121 ;Tests
122 ;(file->list-of-checks (list 1 2.5 3.5 4.5 'NL
123 ; 2 3.5 4.5 5.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
128 ; 2 4.00 18.00 'NL
129 ; 4 2.30 12.50))
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))))