-
Notifications
You must be signed in to change notification settings - Fork 0
/
171.rkt
178 lines (165 loc) · 4.98 KB
/
171.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
;; 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 171ex) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
;; Exercise 171
;; ------------
;; You know what the data definition for List-of-strings looks like. Spell it
;; out. Make sure that you can represent Piet Hein’s poem as an instance of the
;; definition where each line is represented as a string and another instance
;; where each word is a string. Use read-lines and read-words to confirm your
;; representation choices.
;;
;; Next develop the data definition for List-of-list-of-strings. Again,
;; represent Piet Hein’s poem as an instance of the definition where each line
;; is represented as a list of strings, one per word, and the entire poem is a
;; list of such line representations. You may use read-words/line to confirm
;; your choice.
;; -----------------------------------------------------------------------------
(require 2htdp/batch-io)
;; A List-of-strings is one of
;; - '()
;; - (cons String List-of-string)
;; A List-of-list-of-strings is one of
;; - '()
;; - (cons List-of-strings List-of-list-of-strings)
(define lines
(cons "TTT"
(cons ""
(cons "Put up in a place"
(cons "where it's easy to see"
(cons "the cryptic admonishment"
(cons "T.T.T."
(cons ""
(cons "When you feel how depressingly"
(cons "slowly you climb,"
(cons "it's well to remember that"
(cons "Things Take Time."
(cons ""
(cons "Piet Hein" '()))))))))))))))
(equal? lines (read-lines "ttt.txt"))
(define words
(cons
"TTT"
(cons
"Put"
(cons
"up"
(cons
"in"
(cons
"a"
(cons
"place"
(cons
"where"
(cons
"it's"
(cons
"easy"
(cons
"to"
(cons
"see"
(cons
"the"
(cons
"cryptic"
(cons
"admonishment"
(cons
"T.T.T."
(cons
"When"
(cons
"you"
(cons
"feel"
(cons
"how"
(cons
"depressingly"
(cons
"slowly"
(cons
"you"
(cons
"climb,"
(cons
"it's"
(cons
"well"
(cons
"to"
(cons
"remember"
(cons
"that"
(cons
"Things"
(cons
"Take"
(cons
"Time."
(cons
"Piet"
(cons
"Hein"
'()))))))))))))))))))))))))))))))))))
(equal? words (read-words "ttt.txt"))
(define lolos
(cons
(cons "TTT" '())
(cons
'()
(cons
(cons
"Put"
(cons
"up"
(cons "in" (cons "a" (cons "place" '())))))
(cons
(cons
"where"
(cons
"it's"
(cons "easy" (cons "to" (cons "see" '())))))
(cons
(cons
"the"
(cons "cryptic" (cons "admonishment" '())))
(cons
(cons "T.T.T." '())
(cons
'()
(cons
(cons
"When"
(cons
"you"
(cons
"feel"
(cons
"how"
(cons "depressingly" '())))))
(cons
(cons
"slowly"
(cons "you" (cons "climb," '())))
(cons
(cons
"it's"
(cons
"well"
(cons
"to"
(cons "remember" (cons "that" '())))))
(cons
(cons
"Things"
(cons "Take" (cons "Time." '())))
(cons
'()
(cons
(cons "Piet" (cons "Hein" '()))
'()))))))))))))))
(equal? lolos (read-words/line "ttt.txt"))