/
OCaml_lists.ipynb
720 lines (720 loc) · 16.3 KB
/
OCaml_lists.ipynb
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In an effort to start learning some patterns in ML, I decided to go with small steps and reimplement some patterns in OCaml. Assumming the concept of `list` doesn't exist yet we will reimplement a similar concept known as `sequence`:"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"type 'a seq = End | Next of 'a * 'a seq\n"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type 'a seq =\n",
" | End\n",
" | Next of 'a * 'a seq\n",
";;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can define a sequence of zero elements like this:"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"val no_elements : 'a seq = End\n"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"let no_elements = End"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or the count from 1 to 3 like this:"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"val sample : int seq = Next (1, Next (2, Next (3, End)))\n"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"let sample = Next(1, Next(2, Next(3, End)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Implement basic operations\n",
"\n",
"Length, Head, Tail, Cons"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"val length_seq : 'a seq -> int = <fun>\n"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int = 3\n"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"val head_seq : 'a seq -> 'a = <fun>\n"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int = 1\n"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"val tail_seq : 'a seq -> 'a seq = <fun>\n"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int seq = Next (2, Next (3, End))\n"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"val cons_seq : 'a -> 'a seq -> 'a seq = <fun>\n"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int seq = Next (0, Next (1, Next (2, Next (3, End))))\n"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(* length operator *)\n",
"let length_seq x =\n",
" let rec length' acc = function\n",
" | End -> acc\n",
" | Next (_, t) -> length' (acc + 1) t\n",
" in\n",
" length' 0 x\n",
";;\n",
"\n",
"length_seq sample;;\n",
"\n",
"(* head operator *)\n",
"let head_seq = function\n",
" | End -> raise (Invalid_argument \"empty sequence\")\n",
" | Next (h, _) -> h\n",
";;\n",
"\n",
"head_seq sample;;\n",
"\n",
"(* tail operator *)\n",
"let tail_seq = function\n",
" | End -> raise (Invalid_argument \"empty sequence\")\n",
" | Next (_, t) -> t\n",
";;\n",
"\n",
"tail_seq sample;;\n",
"\n",
"(* cons operator *)\n",
"let cons_seq x l = Next(x, l) ;;\n",
"cons_seq 0 sample ;;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can start implementing even more complex operations, like `nth`, `rev`, `append`"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"val nth_seq : int -> 'a seq -> 'a = <fun>\n"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int = 1\n"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int = 3\n"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"val rev_seq : 'a seq -> 'a seq = <fun>\n"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int seq = Next (3, Next (2, Next (1, End)))\n"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"File \"[61]\", line 28, characters 29-60:\n",
"Warning 51: expected tailcall\n"
]
},
{
"data": {
"text/plain": [
"val append_seq : 'a seq -> 'a seq -> 'a seq = <fun>\n"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int seq = Next (1, Next (2, Next (3, Next (4, Next (5, Next (6, End))))))\n"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"val append_seq' : 'a seq -> 'b -> 'b = <fun>\n"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int seq = Next (1, Next (2, Next (3, Next (4, Next (5, Next (6, End))))))\n"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"val ( +: ) : 'a -> 'a seq -> 'a seq = <fun>\n"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int seq = Next (0, Next (1, Next (2, Next (3, End))))\n"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(* nth operator *)\n",
"let rec nth_seq n l = \n",
" if n < 0 then raise (Invalid_argument \"nth\") else\n",
" match l with\n",
" | End -> raise (Failure \"nth\")\n",
" | Next (x, tl) -> if n = 0 then x else nth_seq (n - 1) tl\n",
";;\n",
"\n",
"nth_seq 0 sample ;;\n",
"nth_seq 2 sample ;;\n",
"\n",
"(* rev operator *)\n",
"let rev_seq l =\n",
" let rec rev' acc = function\n",
" | End -> acc\n",
" | Next (x, tl) -> rev' (cons_seq x acc) tl\n",
" in\n",
" rev' End l\n",
";;\n",
"\n",
"rev_seq sample ;;\n",
"\n",
"(* append operator, this function is not tail recursive *)\n",
"(* the original in OCaml neither *)\n",
"let rec append_seq a b =\n",
" match a with\n",
" | End -> b\n",
" | Next (x, tl) -> Next(x, ((append_seq [@tailcall]) tl b))\n",
";;\n",
"\n",
"append_seq sample (Next(4, Next(5, Next(6, End))))\n",
"\n",
"(* We can make a recursive append *)\n",
"let append_seq' a b =\n",
" let rec append' acc = function\n",
" | End -> b\n",
" | Next (x, t) -> append' (Next(x, acc)) t\n",
" in\n",
" append' End (rev_seq a)\n",
";;\n",
"\n",
"append_seq sample (Next(4, Next(5, Next(6, End)))) ;;\n",
"\n",
"let (+:) a b = cons_seq a b ;;\n",
"0 +: sample"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can start playing with functions and lists. What about `init`, `map` and, more importantly, `fold_right` and `fold_left`?"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"val init_seq : int -> (int -> 'a) -> 'a seq = <fun>\n"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int seq = Next (1, Next (2, Next (3, End)))\n"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"val map_seq : ('a -> 'b) -> 'a seq -> 'b seq = <fun>\n"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"File \"[62]\", line 13, characters 32-59:\n",
"Warning 51: expected tailcall\n"
]
},
{
"data": {
"text/plain": [
"- : int seq = Next (2, Next (4, Next (6, End)))\n"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"val map_seq : f:('a -> 'b) -> 'a seq -> 'b seq = <fun>\n"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int seq = Next (2, Next (4, Next (6, End)))\n"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(* init operator *)\n",
"let init_seq n f =\n",
" let rec init' n' acc =\n",
" if n' = 0 then acc else init' (n' - 1) (cons_seq (f n') acc)\n",
" in\n",
" init' n End\n",
";;\n",
"\n",
"init_seq 3 (fun x -> x) ;;\n",
"\n",
"(* map operator, not tail recursive *)\n",
"let rec map_seq f = function\n",
" | Next (x, t) -> Next ((f x), ((map_seq [@tailcall]) f t))\n",
" | _ -> End\n",
";;\n",
"\n",
"map_seq (fun x -> x * 2) sample ;;\n",
"\n",
"(* map operator, now tail recursive *)\n",
"let map_seq ~f l =\n",
" let rec map_seq' acc = function\n",
" | End -> acc\n",
" | Next (x, t) -> map_seq' (Next((f x), acc)) t\n",
" in\n",
" rev_seq (map_seq' End l)\n",
";;\n",
"\n",
"map_seq ~f:(fun x -> x * 2) sample"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now it is time for the `fold` functions. The fold functions is just a nicer way to say \"aggregate\" functions."
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"val fold_left_seq : f:('a -> 'b -> 'a) -> init:'a -> 'b seq -> 'a = <fun>\n"
]
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int = 6\n"
]
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"let rec fold_left_seq ~f ~init = function\n",
" | End -> init\n",
" | Next (x, tl) -> (fold_left_seq [@tailcall]) ~f ~init:(f init x) tl\n",
";;\n",
"\n",
"fold_left_seq ~f:(fun a b -> a + b) ~init:0 sample"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"the contrapart of `fold_left_seq` is `fold_right_seq`, we can implement it in two ways, a non-tail recursive and a tail recursive version, let's see both:"
]
},
{
"cell_type": "code",
"execution_count": 143,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"File \"[143]\", line 5, characters 24-66:\n",
"Warning 51: expected tailcall\n"
]
},
{
"data": {
"text/plain": [
"val fold_right_seq : f:('a -> 'b -> 'b) -> 'a seq -> init:'b -> 'b = <fun>\n"
]
},
"execution_count": 143,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"val fold_right_seq : f:('a -> 'b -> 'b) -> 'a seq -> init:'b -> 'b = <fun>\n"
]
},
"execution_count": 143,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(* Non tail recursive fold_right *)\n",
"let rec fold_right_seq ~f l ~init =\n",
" match l with\n",
" | End -> init\n",
" | Next (x, tl) -> f x ((fold_right_seq [@tailcall]) ~f tl ~init)\n",
";;\n",
"\n",
"(* tail recursive fold_right *)\n",
"(* Notice we have to _reverse_ the list and change the signature of ~f *)\n",
"let rec fold_right_seq ~f l ~init = \n",
" fold_left_seq ~f:(fun a b -> f b a) (rev_seq l) ~init ;;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When to use which? well, it depends, in _theory_ the non-tail recursive is more efficient but of course it cannot work in long sequences, being $O(n)$ while the tail recursive is $O(n^2)$. **Remember**, `cons_seq` is $O(1)$, while `rev_seq` is $O(n)$ (the same as `fold_left_seq`) and when using _reverse inner accumulator loop_ we get $O(2n)$ which is basically $O(n)$\n",
"\n",
"The difference between both is about memory and performance. For _large sequences_ our tail recursive version is the only option while for short sequences is prefered to use the non tail recursive.\n",
"\n",
"We can implement other basic sequence functions based in `fold_left`, for example, the always important function `rev`"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"val rev_seq' : '_weak8 seq -> '_weak8 seq = <fun>\n"
]
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int seq = Next (3, Next (2, Next (1, End)))\n"
]
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"let rev_seq' = \n",
" fold_left_seq ~f:(fun l' x -> cons_seq x l') ~init:End \n",
";;\n",
"\n",
"rev_seq' sample"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice how we are _currying_ the function definition, because _why not?_.\n",
"\n",
"More complex functions can be represented with fold as well, for example, `map`"
]
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"val map_seq' : f:('a -> 'b) -> 'a seq -> 'b seq = <fun>\n"
]
},
"execution_count": 144,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int seq = Next (1, Next (2, Next (3, End)))\n"
]
},
"execution_count": 144,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : int seq = Next (2, Next (4, Next (6, End)))\n"
]
},
"execution_count": 144,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"let map_seq' ~f =\n",
" fold_right_seq ~f:(fun elem l -> cons_seq (f elem) l) ~init:End\n",
";;\n",
"sample ;;\n",
"map_seq' ~f:(fun x -> x * 2) sample"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "OCaml 4.06.0",
"language": "OCaml",
"name": "ocaml-jupyter"
},
"language_info": {
"codemirror_mode": "text/x-ocaml",
"file_extension": ".ml",
"mimetype": "text/x-ocaml",
"name": "OCaml",
"nbconverter_exporter": null,
"pygments_lexer": "OCaml",
"version": "4.06.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}