forked from ucsd-cse230/01-trees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Doc.hs
216 lines (179 loc) · 5.13 KB
/
Doc.hs
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
module CSE230.Doc
(
-- * A document type
Doc
-- * Constructors
, empty, doc
-- * Accessors
, width, height
-- * Example Doc
, aDoc, bDoc, lineDoc, animals, triangles
-- * Combinators
, vcatL, vcatR, hcatB, hcatT
-- * Properties
, prop_hcatT_width
, prop_hcatT_height
, prop_hcatB_width
, prop_hcatB_height
, prop_vcatL_width
, prop_vcatR_width
, prop_vcatL_height
, prop_vcatR_height
) where
import qualified Test.QuickCheck as QC
import Prelude hiding (maximum)
import CSE230.List
-------------------------------------------------------------------------------
-- | A 'Doc' is a 'String' list
-------------------------------------------------------------------------------
data Doc = D { docLines :: [String] }
deriving (Eq, Ord)
empty :: Doc
empty = D []
doc :: String -> Doc
doc s = D (lines s)
aDoc :: Doc
aDoc = D [ "a"
, "aaa"
, "aaaaa"]
bDoc :: Doc
bDoc = D [ "b"
, "bbb"]
lineDoc :: Doc
lineDoc = doc "<----- HERE"
animals :: [Doc]
animals = [ doc "cat"
, doc "horse"
, doc "mongoose"
]
-------------------------------------------------------------------------------
-- | Printing a Doc
-------------------------------------------------------------------------------
-- >>> aDoc
-- a
-- aaa
-- aaaaa
-- <BLANKLINE>
--
instance Show Doc where
show (D ls) = unlines ls
-------------------------------------------------------------------------------
-- | Generating Random Docs
-------------------------------------------------------------------------------
instance QC.Arbitrary Doc where
arbitrary = fmap D QC.arbitrary
-------------------------------------------------------------------------------
-- | Dimensions of a 'Doc'
-------------------------------------------------------------------------------
-- >>> width beers
-- 11
width :: Doc -> Int
width (D ls) = maximum 0 (map length ls)
-- >>> height beers
-- 3
height :: Doc -> Int
height (D ls) = length ls
-------------------------------------------------------------------------------
-- | Vertical Concatenation aligned at Left
-------------------------------------------------------------------------------
-- >>> (doc "cat") `vcatL` (doc "horse") `vcatL` (doc "mongoose")
-- cat
-- horse
-- mongoose
-- <BLANKLINE>
--
vcatL :: Doc -> Doc -> Doc
vcatL d1 d2 = error "fill this in"
-------------------------------------------------------------------------------
-- | Vertical Concatenation aligned at Right
-------------------------------------------------------------------------------
-- >>> (doc "cat") `vcatR` (doc "horse") `vcatR` (doc "mongoose")
-- cat
-- horse
-- mongoose
-- <BLANKLINE>
--
vcatR :: Doc -> Doc -> Doc
vcatR d1 d2 = error "fill this in"
-------------------------------------------------------------------------------
-- | Horizontal Concatenation aligned at Top
-- HINT: use `zip` or `zipWith`
-------------------------------------------------------------------------------
-- >>> hcatT aDoc lineDoc
-- a <----- HERE
-- aaa
-- aaaaa
-- <BLANKLINE>
--
-- >>> hcatT aDoc bDoc
-- a b
-- aaa bbb
-- aaaaa
-- <BLANKLINE>
--
hcatT :: Doc -> Doc -> Doc
hcatT d1 d2 = error "fill this in"
elongate :: Dir -> Int -> Doc -> Doc
elongate dir h (D ls) = D (pad dir h "" ls)
-------------------------------------------------------------------------------
-- | Horizontal Concatenation aligned at Bottom
-- HINT: use `zip` or `zipWith`
-------------------------------------------------------------------------------
-- >>> hcatB aDoc lineDoc
-- a
-- aaa
-- aaaaa<----- HERE
-- <BLANKLINE>
--
-- >>> hcatB aDoc bDoc
-- a
-- aaa b
-- aaaaabbb
-- <BLANKLINE>
--
hcatB :: Doc -> Doc -> Doc
hcatB d1 d2 = error "fill this in"
triangle :: Doc
triangle = D
[ "*"
, "***"
, "*****" ]
-- >>> foldr vcatL empty triangles
-- *
-- ***
-- *****
-- * *
-- *** ***
-- **********
-- * * *
-- *** *** ***
-- ***************
-- <BLANKLINE>
--
-- >>> foldr vcatR empty triangles
-- *
-- ***
-- *****
-- * *
-- *** ***
-- **********
-- * * *
-- *** *** ***
-- ***************
-- <BLANKLINE>
--
triangles :: [Doc]
triangles = [ triangle
, triangle `hcatT` triangle
, triangle `hcatT` triangle `hcatT` triangle ]
-------------------------------------------------------------------------------
-- | Properties of `Doc` combinators ------------------------------------------
-------------------------------------------------------------------------------
prop_hcatT_width d1 d2 = width (d1 `hcatT` d2) == width d1 + width d2
prop_hcatT_height d1 d2 = height (d1 `hcatT` d2) == max (height d1) (height d2)
prop_hcatB_width d1 d2 = width (d1 `hcatB` d2) == width d1 + width d2
prop_hcatB_height d1 d2 = height (d1 `hcatB` d2) == max (height d1) (height d2)
prop_vcatL_width d1 d2 = width (d1 `vcatL` d2) == max (width d1) (width d2)
prop_vcatR_width d1 d2 = width (d1 `vcatR` d2) == max (width d1) (width d2)
prop_vcatL_height d1 d2 = height (d1 `vcatL` d2) == height d1 + height d2
prop_vcatR_height d1 d2 = height (d1 `vcatR` d2) == height d1 + height d2