-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcantorset.hs
349 lines (213 loc) · 4.74 KB
/
cantorset.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
-- we'll just define a continuous set of points by the bounding pts
-- i.e. the set [0,1] will similairly be denoted [0,1]
-- to construct the cantor set there are two basic functions: divide the current space in to three spaces, and make each space one third the size of the orignal space
-- [0,1] -> [[0,(1/3)],[(1/3),(2/3)],[(2/3),1]]
-- -> [[0,(1/9)],[(2/9),(1/3)],[(2/3),(7/9)],[8/9,9]]
-- the real data structure we want to generate is a tree, but a flattened list will be easier to visualize
-- 1/3,1/9,
--0,1
--0,1,2,3
--0,1,2,3,6,7,8,9
--0,1,2,3,6,7,8,9,18?
--so we are squaring the number of
1. Divide by 3
2. Concat (1) * 2
let divby3 (x:xs) = map (*(1/3)) (x:xs)
let a = divby3 [0,1]
map (+(1/3)) [0,1]
let y = fromIntegral 1 / 3 :: Rational
y
:{
let z :: Rational
z = fromIntegral 4 / 9
:}
:{
let z2 :: Rational
z2 = fromIntegral 4 / 10
:}
z2 + z + y
(1/38) + z
:{
let e :: Rational
e = 3 / 4
:}
e
let f = fromIntegral 3 / 4
f
-- do it preseving the rational form
let yy n = fromIntegral 1 / (3^^n) :: Rational
yy 2
map (+y) [0,1]
map (\n -> (map (+(n/3)) [0,(1/3)])) [0,1,2]
let erkj p = map (\n -> (map (+(n/9)) p )) [0,2]
-- erkj takes the interval and maps it to
let erkj p a = map (\n -> (map (+(n/(3^^a))) p )) [0,2]
erkj [0,(1/3)] 1
erkj [0,(1/9)] 2
-- l === [0,1]
:{
let tr a 0 l = []
tr a n l = map (map (tr (a+1) (n-1))) (erkj p a)
where p = divby3 l
:}
let x = divby3 [0,1] in erkj x 1
-- this gets the correct second value
map (map (+ (2/3))) (erkj [0,(1/9)] 9)
map (map (+ (2/3))) (erkj [0,(1/27)] 27)
(erkj [0,(1/27)] 27)
1/27
erkj [0,(1/9)] 9
map (+(2/3)) (erkj [0,(1/9)] 9)
map (+ (2/3)) (head $ tail $ erkj [0,(1/9)] 9)
erjk [
:{
let asdf x m = erkj y (3^^m)
where y = divby3 x
:}
asdf [0,1] 2
let y = fromIntegral 1 / 3 :: Rational
y
erkj [0,(1/3)] 3
erkj [(2/3),1] 3
erkj [(2/3),1] 9
asdf [(2/3),1] 2
asdf [0,(1/3)]
divby3 [0,1]
erkj [0,1/3] 3
erkj [0,(1/9)]
erkj [(2/3),1]
-- not quite
let partitionSet x = map (\n -> (map (+(n/x)) [0,(1/x)])) [0,2]
partitionSet 3
partitionSet 9
map (\n -> (map (+(n/9)) [0,(1/9)])) [0,2]
((\n -> map (+(n/3))) [0.1,2]) 3
map (+
divInto3
map (*(1/3)) [0,1]
-- wrong
-- let peoir p y = map (\n -> erkj n y) p
-- peoir [[0,(1/3)],[(2/3),1]] 3
:{
let union1 x [] = [x]
union1 x y'@(y:ys)
| x == y = y'
| otherwise = y : union1 x ys
:}
union1 3 [1..4]
:{
let isElem x [] = False
isElem x y'@(y:ys)
| x == y = True
| otherwise = isElem x ys
:}
isElem 5 [1..4]
:{
let isElemVal x y
| isElem x y = []
| otherwise = [x]
:}
isElemVal 34 [1..5]
:{
let union x'@(x:xs) y'@(y:ys) = foldl (++) [] (iev ++ [y'])
where iev = map (\v -> isElemVal v y') x'
:}
union [3..5] [1,4,6]
union ['a'..'d'] ['z']
union [[1..5],[1..3]] [[1,3],[1..5]]
union [5,1,4] [3,4,5]
-- finally, geez. union and concat are interchangeable, presumably the latter uses less memory
:{
let cantor 0 = [0,1]
cantor n = union cnm1o3 (map (+(2/3)) cnm1o3)
where cnm1o3 = map (*(1/3)) cnm1
cnm1 = cantor (n-1)
:}
:t cantor
:{
let cantor :: Int -> [Rational]
cantor 0 = [0,1]
cantor n = union cnm1o3 (map (+(2/3)) cnm1o3)
where cnm1o3 = map (*(1/3)) cnm1
cnm1 = cantor (n-1)
:}
let op = cantor 3
:t op
let po = map fromRational op
op
map toRational op
let basdf = ((1/3) + (40/59))
:{
let basdf :: Rational
basdf = ((1/3) + (40/59))
:}
basdf
toRational basdf
:{
let b :: Rational -> Fractional
b a = (1/2) + a
:}
toFractional (3/2)
:t toRational
fromRational (3 % 2)
:t (3/2)
:t (2 % 3)
toRational (3/2)
b
:t (1/2)
cantor 3
:{
let cantor2 0 = [0,1]
cantor2 n = (++) cnm1o3 (map (+(2/3)) cnm1o3)
where cnm1o3 = map (*(1/3)) cnm1
cnm1 = cantor2 (n-1)
:}
cantor2 0
cantor2 3
let c2 = cantor 2
cantor 5
c2
2*(1/3)
4*(1/9)
let o3toPn n = (2^n) / (3^n)
-- the cutoff point
o3toPn 646
let oneOn n = 1/n
oneOn (3^10090)
3^100903324
-- all nice looking, but irrelevant in this case
:{
let pairList [] = []
pairList (x:y:xs) = pairs x y : pairList xs
where pairs x y = [x,y]
:}
let pc2 = pairList c2
sumPairList pc2
:{
let sumPairList xs = map binDiff xs
where binDiff [x,y] = y - x
:}
cantor 3
-- nub deletes redundant elements
:t nub
nub [1,4,3,2,34]
:t delete
delete 3 [1..5]
-- inadvertently defines intersection?
let union x'@(x:xs) y'@(y:ys) = map (\v -> isElemVal v y') x'
union [3..5] [1..10]
let zz = union [3..5] [1..10]
zz ++ [[3]]
-- original definiton
-- :{
-- let isElemVal x y
-- | isElem x y = [x]
-- | otherwise = []
-- :}
:t flip
flip isElem [1..10] 1
map (flip isElem [1..4]) [1..10]
map (\s -> union1 s [1..4]) [1..10]
let un x'@(x:xs) y'@(y:ys) = map (\v -> union1 v y') x'
:t un
un [1..5] [5..10]