-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurround.hs
More file actions
304 lines (217 loc) · 5.26 KB
/
Copy pathsurround.hs
File metadata and controls
304 lines (217 loc) · 5.26 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
:{
let insertAt :: a -> Int -> [a] -> [a]
insertAt newElement _ [] = [newElement]
insertAt newElement i (a:as)
| i <= 1 = newElement:a:as
| otherwise = a : insertAt newElement (i - 1) as
:}
insertAt 3 4 [1..10]
:{
let insertAtEnd a [] = [a]
insertAtEnd a (x:xs) = x : insertAtEnd a xs
:}
insertAtEnd 'c' "asdf"
let surround x ys = x : insertAtEnd x ys
surround 'c' "asdf"
easierSurround x ys = x : ys ++ [x]
easierSurround 3 [1..4]
:{
let myConcat [] [] = []
myConcat [] (y:ys) = y : myConcat [] ys
myConcat (x:xs) (y:ys) = x : myConcat xs (y:ys)
:}
myConcat "asdf" "fdsa"
-- a surround with my own concatonation defined
let beforeAndAfter x y = myConcat x $ myConcat y x
beforeAndAfter "asdf" "fdsa"
-- e.g. belongsTo
:{
let questionMark x [] = False
questionMark x (y:ys)
| x == y = True
| otherwise = questionMark x ys
:}
questionMark 3 [1..5] == True
questionMark 3 [4..5] == False
:{
let q2 xs [] = False
q2 [] ys = True
q2 (x:xs) (y:ys)
-- wrong right here, need an extra condition if its not correct
| x == y = q2 xs ys
| otherwise = q2 (x:xs) ys
:}
-- matches if there is an a somewhere followed by a b anywhere else after it. There's some regexp for this
q2 "ab" "aabc"
q2 "ab" "aacbc"
q2 "ab" "cbca"
questionMark 'a' "bc"
questionMark 'a' "bcaa"
:{
let q2 xs [] = False
q2 [] ys = True
q2 (x:xs) (y:ys)
-- wrong right here, need an extra condition if its not correct
| x == y = q2 xs ys
| otherwise = q2 (x:xs) ys
:}
-- doesn't work, don't care to figure out why
-- :{
-- let q2 xs [] = False
-- q2 z@(x:xs) (y:ys)
-- | z == [] = True
-- | x == y = q2 xs ys
-- | otherwise = q2 z ys
-- :}
:{
data Tree a = Empty | Branch a (Tree a) (Tree a)
deriving (Show, Eq)
:}
let leaf x = Branch x Empty Empty
:{
let cbalTree :: Int -> [Tree Char]
cbalTree 0 = [Empty]
cbalTree n = let (q, r) = quotRem (n - 1) 2
in [Branch 'x' left right | i <- [q .. q + r],
left <- cbalTree i,
right <- cbalTree (n - i - 1)]
:}
:{
let qR :: Int -> Int -> (Int, Int)
qR x y = (quot0 x y, myMod x y)
:}
map (qR 100) [1..20]
:{
let quot' x y
| (x >= y) = quot' (x-y) y
| otherwise = x
:}
:{
let myMod x y
| (x < y) = x
| otherwise = myMod (x - y) y
:}
-- x bigger, y smaller
myMod 55 11
myMod 55 13
map (myMod 55) [1..11]
-- quotient? Indepenent of
:{
let quot x y n
| x < y = n
| otherwise = quot (x-y) y (n+1)
:}
quot 18 6 0
quot x y = n
where n
| x > y = 1 + quot (x-y) y
| otherwise = n
myQuot :: Int -> Int -> Int
myQuot x y = go x 0
where
go x n
| x < y = n
| otherwise = go (x - y) (n + 1)
:{
let myQuot :: Int -> Int -> Int
myQuot x y = go x 0
where
go x n
| d < 0 = n
| otherwise = go d (n + 1)
where d = x - y
:}
:{
let myQuot :: Int -> Int -> Int
myQuot x y = go x 0
where
go x n
| d < 0 = n
| otherwise = go d (n + 1)
where d = x - y
:}
myQuot 100 3
quot :: Int a => a -> a -> a -> a
quot 100 3 0
let quot0 x y = quot x y 0
:{
let count :: (a -> Bool) -> [a] -> Int
count _ [] = 0
count p (x:xs)
| p x = 1 + count p xs
| otherwise = count p xs
:}
qt x y
| x < y = 1 + qt (x-y) y
| otherwise = 1 + qt (x-y) y
count (>3) [1..100]
l
myMod 100 3
rem 30 2
quot 100 3
:t qR
qR 10 2
map (qR 100) [1..10]
map (quotRem 100) [1..10]
:{
let pl3 :: Int -> Int
pl3 x = x + 3
:}
:{
let cbalTree 0 = [Empty]
cbalTree 1 = [leaf 'x']
cbalTree n = if n `mod` 2 == 1 then
[ Branch 'x' l r | l <- cbalTree ((n - 1) `div` 2),
r <- cbalTree ((n - 1) `div` 2) ]
else
concat [ [Branch 'x' l r, Branch 'x' r l] | l <- cbalTree ((n - 1) `div` 2),
r <- cbalTree (n `div` 2) ]
:}
cbalTree 5
:{
let freeTree :: Tree Char
freeTree =
Node 'P'
(Node 'O'
(Node 'L'
(Node 'N' Empty Empty)
(Node 'T' Empty Empty)
)
(Node 'Y'
(Node 'S' Empty Empty)
(Node 'A' Empty Empty)
)
)
(Node 'L'
(Node 'W'
(Node 'C' Empty Empty)
(Node 'R' Empty Empty)
)
(Node 'A'
(Node 'A' Empty Empty)
(Node 'C' Empty Empty)
)
)
:}
freeTree
mirror x y =
:{
let mirror Empty Empty = True
mirror (Branch _ a b) (Branch _ x y) = mirror a y && mirror b x
mirror _ _ = False
:}
:t mirror
symmetric x =
:{
data Tree a = Empty | Branch a (Tree a) (Tree a)
deriving (Show, Eq)
:}
let leaf x = Branch x Empty Empty
symmetric (Branch 'x' (Branch 'x' Empty Empty) (Branch 'x' Empty Empty))
myQuot :: Int -> Int -> Int
myQuot x y = go x 0
where
go x n
| d < 0 = n
| otherwise = go d (n + 1)
where d = x - y