-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCM20210821_SICP_2.1.1_ArithmeticOperationsForRationalNumbers.jl
2245 lines (1859 loc) · 86.8 KB
/
PCM20210821_SICP_2.1.1_ArithmeticOperationsForRationalNumbers.jl
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
### A Pluto.jl notebook ###
# v0.19.12
using Markdown
using InteractiveUtils
# ╔═╡ 7d9a4133-99cf-40f4-9920-c0425f6676f3
using Plots
# ╔═╡ e8c1f880-0278-11ec-2538-13bb2f14d606
md"
======================================================================================
#### SICP: [2.1.1 Arithmetic Operations For Rational Numbers](https://sarabander.github.io/sicp/html/2_002e1.xhtml#g_t2_002e1_002e1)
###### file: PCM20210821\_SICP\_2.1.1\_ArithmeticOperationsForRationalNumbers.jl
###### Julia/Pluto.jl-code (1.8.2/0.19.14) by PCM *** 2023/02/22 ***
======================================================================================
"
# ╔═╡ 6011e641-b628-4642-8c55-549b05efbb89
md"
##### Relations between *number* sets
$$\mathbb N \subset \mathbb Z \subset \mathbb Q \subset \mathbb R \subset \mathbb C$$
More on *rational numbers* $$\mathbb Q$$ can be found [here](https://en.wikipedia.org/wiki/Rational_number).
---
"
# ╔═╡ 2b87595b-9491-4ebe-acb9-1ab7d43c4678
md"
#### 2.1.1.1 SICP-Scheme-like *functional*, mostly *un*typed Julia
###### ... with *imperative* construct $$while$$
"
# ╔═╡ 55e8fb6e-4f94-46f2-97ff-42bfdcd37a48
md"
We represent rational numbers as *pairs* of numerator and denominators in four alternative ways:
- the *first* is the *default* way, very *Scheme-like* by an *un*typed *named tuple* with two fields $$car$$ and $$cdr$$; the construction is deferred into the body of a constructor function $$cons$$.
- the *second* is the first *specialized* way by using Julia's *built-in* type $$Pair$$. The construction of pairs is deferred to the body of a constructor function $$cons2$$. The two fields can accessed by $$first$$ and $$second$$. They are hidden in the *Scheme*-like selector functions $$car2$$ and $$cdr2$$.
- the *third* is the second *specialized* way by using Julia's *built-in* type $$Rational$$. The construction of pairs is deferred to the body of a *Scheme*-like constructor function $$cons3$$. The two fields can accessed by the functions $$numerator$$ and $$denominator$$. They are hidden in the *Scheme*-like selector functions $$car3$$ and $$cdr3$$.
- the *fourth* is the most easy and third *specialized* way by using Julia's *built-in* type $$Rational$$ and its *built-in* operators $$+, -, *, /$$, and $$==$$. The construction of rationals is done by using Julia's *built-in* constructon operator $$//$$. The two fields can accessed by $$numerator$$ and $$denominator$$.
In this chapter we do *not* exploit Julia's *multiple dispatch*. So all functions have slightly different names depending on the number of the alternative. *Multiple dispatch* is valuable for subordinating alternative *functions* as *methods* under the umbrella of a reduced set of function objects.
"
# ╔═╡ d4e48294-c66c-4fa3-9586-89fb825a4ee8
md"
##### 2.1.1.1.1 Arithmetic Operations for Rational Numbers
###### *1st* methods (*default*, *un*typed) of functions $$add\_rat$$, $$sub\_rat$$, $$mul\_rat$$, $$div\_rat$$, $$equal\_rat$$
"
# ╔═╡ dc723ee4-97ce-41a3-88b0-49ccb31c289e
md"
---
###### Addition
$$+\;\;: \;\; \mathbb Q \times \mathbb Q \rightarrow \mathbb Q$$
$$x+y = \frac{n_x}{d_x}+\frac{n_y}{d_y}= \frac{n_x d_y + n_y d_x}{d_x d_y}$$
---
"
# ╔═╡ 68d7c3b8-d2b6-49d5-9fcc-7a69100e7270
md"
---
###### Subtraction
$$-\;\; : \;\; \mathbb Q \times \mathbb Q \rightarrow \mathbb Q$$
$$x-y = \frac{n_x}{d_x}-\frac{n_y}{d_y}= \frac{n_x d_y - n_y d_x}{d_x d_y}$$
---
"
# ╔═╡ bf2c6ebd-8b13-47c2-b11c-0cbff01978ca
md"
---
###### Multiplication
$$\cdot\;\;: \;\; \mathbb Q \times \mathbb Q \rightarrow \mathbb Q$$
$x \cdot y = \frac{n_x}{d_x} \cdot \frac{n_y}{d_y}= \frac{n_x n_y}{d_x d_y}$
---
"
# ╔═╡ 0875ee4d-e127-4140-a863-04c3a02c38a5
md"
---
###### Division
$$/\;\;: \;\; \mathbb Q \times \mathbb Q \rightarrow \mathbb Q$$
$$x/y = \left(\frac{n_x}{d_x}\right)/\left(\frac{n_y}{d_y}\right) = \frac{n_x d_y}{d_x n_y}$$
---
"
# ╔═╡ f74ced3c-487c-4428-bcfc-cc69e5416227
md"
---
###### Equality test
$$=\;\;: \;\; \mathbb Q \times \mathbb Q \rightarrow \mathbb B$$
$$(x=y) \equiv \left(\frac{n_x}{d_x} = \frac{n_y}{d_y}\right) \equiv (n_x d_y = d_x n_y)$$
---
"
# ╔═╡ 08ede030-fb83-4337-8c37-f346b1b7ebc0
md"
---
##### 2.1.1.1.2 Pairs (... as *Named* Tuples)
###### Scheme's pair constructor $$cons$$ and Selectors $$car$$ and $$cdr$$ as Julia-*functions* and -*tuples* with *named* fields.
The *interface* of pair-related functions $cons, cadr$, and $cdr$ is similar to *[M-expressions](https://en.wikipedia.org/wiki/M-expression)* (= *Meta-language* expressions) introduced by *McCarthy* et al. (1965, p.9).
...*there are two LISP's: there is the algorithmic language and there is the programming language. The programing language is a data structure representation of the algorithmic language. The algorithmic language is called the* **meta-language** *or* **M-expr LISP**, *and for historical purposes, the programming language is called* **[S-expr](https://en.wikipedia.org/wiki/S-expression) LISP** (Allen, J., 1978, p.107).
So the *interface* of our Scheme-like Julia-functions resembles in form and function expressions of *McCarthy's meta-language*.
The three different signatures are:
"
# ╔═╡ 80ed0fe4-9f5b-4521-8189-f7f1273706fb
md"
$$\begin {array}{c|c|c}
& \text{Language Level} & \\
\hline
\text{algorithmic:} & \text{programming:} & \text{programming:} \\
\text{M-exprs} & \text{S-exprs} & \text{Julia-exprs} \\
\text{McCarthy, Allen} & \text{Lisp, Scheme} & \text{Julia} \\
\hline
\text{cons[<arg1>,<arg2>]} & \text{(cons <arg1> <arg2>)}&\text{cons(<arg1>,<arg2>)} \\
\hline
\text{car[cons[<a1>,<a2>]]}&\text{(car(cons <a1> <a2>))}&\text{car(cons(<a1>,<a2>))}\\
\text{==<a1>} & \text{==<a1>} & \text{==<a1>} \\
\hline
\text{cdr[cons[<a1>,<a2>]]}&\text{(cdr(cons <a1> <a2>))}&\text{cdr(cons(<a1>,<a2>))}\\
\text{==<a2>} & \text{==<a2>} & \text{==<a2>} \\
\hline
\end {array}$$
**Fig. 2.1.1.1**: Abstraction levels for basic *constructor* and *selector* functions $cons, car$, and $cdr$
---
"
# ╔═╡ d2d4dd60-f2e4-421c-80cd-63fb61faf31c
md"
$$\begin {array}{c|c|c}
& \text{Abstraction Hierarchy} & \\
\hline
\text{ Abstract} & addRat, subRat & \text{level 2} \\
\text{Operators} & mulRat, divRat, equalRat & \text{Domain} \\
& printRat & \\
\hline
\text{Constructor/} & makeRat & \text{level 1} \\
\text{Selectors} & numer, denom & \\
\hline
\text{Constructor/} & cons & \text{level 0} \\
\text{Selectors} & car, cdr & \textit{Scheme}\text{-like} \\
\hline
\text{Constructor/} & consCell = (car = ... , cdr = ...) & \text{level -1} \\
\text{Selectors} & consCell.car, consCell.cdr & \textit{Julia} \\
\hline
\end {array}$$
**Fig. 2.1.1.2**: *First* default abstraction hierarchy for *rational* number algebra or [here](https://sarabander.github.io/sicp/html/2_002e1.xhtml#g_t2_002e1))
---
"
# ╔═╡ 1c6a666d-c388-4ac9-b65f-dcebf9fc082f
md"
###### *1st* method (*default*, *un*typed) of function $$cons, car$$, and $$cdr$$
definition of constructor $$cons$$ with named fields $$car$$ and $$cdr$$
"
# ╔═╡ 3b7faab0-b8e0-4bea-894f-16fd35e5727f
cons(car, cdr) = (car=car, cdr=cdr) # cons(<par1>, <par2>) = (car=<par1>, cdr=<par2>)
# ╔═╡ 80fdcc6e-3435-4c3a-a73a-b4ecf54ecfb2
car(cons) = cons.car # definition of selector 'car'
# ╔═╡ 1c695d3b-c88e-4227-8aa1-e88149a4f9f4
cdr(cons) = cons.cdr # definition of selector 'cdr'
# ╔═╡ 3e8c66d9-f3ce-4235-b096-6544daf9632f
let
yMax = 3
#------------------------------------------------------------------------------
function plotInvUnaryTree!(markOfLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; heightOfTree=2, fontSize=9)
plot!([ # plot of middle vertical arm '|'
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree/8), # root
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree+heightOfTree/8)], # leaf
lw=1, linecolor=:black)
annotate!( # mark of leaf 'x'
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree, text(markOfLeaf, fontSize, :blue)))
annotate!( # mark of root 'x'
(coordinateXOfRootMark, coordinateYOfRootMark, text(markOfRoot, fontSize, :blue)))
end # function plotUnaryTree!
#------------------------------------------------------------------------------
function plotInvBinaryTree!(markOfLeftLeaf, markOfRightLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=1.5, heightOfTree=2, fontSize=9)
annotate!( # mark of left leaf 'x'
(coordinateXOfRootMark-widthOfTree/2,
coordinateYOfRootMark-heightOfTree,
text(markOfLeftLeaf, fontSize, :blue)))
plot!([ # plot of left vertical arm '|'
(coordinateXOfRootMark-widthOfTree/2,
coordinateYOfRootMark-heightOfTree+heightOfTree/8),
(coordinateXOfRootMark-widthOfTree/2, coordinateYOfRootMark-heightOfTree/2)],
lw=1, linecolor=:black)
annotate!( # mark of right leaf 'x'
(coordinateXOfRootMark+widthOfTree/2,
coordinateYOfRootMark-heightOfTree,
text(markOfRightLeaf, fontSize, :blue)))
plot!([ # plot of right vertical arm '|'
(coordinateXOfRootMark+widthOfTree/2,
coordinateYOfRootMark-heightOfTree+heightOfTree/8),
(coordinateXOfRootMark+widthOfTree/2, coordinateYOfRootMark-heightOfTree/2)],
lw=1, linecolor=:black)
plot!([ # plot of horizontal bar '-'
(coordinateXOfRootMark-widthOfTree/2, coordinateYOfRootMark-heightOfTree/2),
(coordinateXOfRootMark+widthOfTree/2, coordinateYOfRootMark-heightOfTree/2)],
lw=1, linecolor=:black)
plot!([ # plot of middle vertical arm '|'
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree/2),
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree/8)],
lw=1, linecolor=:black)
annotate!( # mark of root leaf 'x'
(coordinateXOfRootMark, coordinateYOfRootMark, text(markOfRoot, fontSize, :blue)))
end # function plotBinaryTree!
#------------------------------------------------------------------------------
function plotInvTernaryTree!(markOfLeftLeaf, markOfMiddleLeaf, markOfRightLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=3.0,
heightOfTree=2, fontSize=9)
#---------------------------------------------------------
plotInvBinaryTree!(markOfLeftLeaf, markOfRightLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=widthOfTree, heightOfTree=heightOfTree, fontSize=fontSize)
#---------------------------------------------------------
plotInvUnaryTree!(markOfMiddleLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; heightOfTree=heightOfTree, fontSize=fontSize)
end # function plotTernaryTree!
#------------------------------------------------------------------------------
function plotInvQuaternyTree!(markOfLeftMostLeaf, markOfLeftLeaf, markOfRightLeaf, markOfRightMostLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=4.0, heightOfTree=2, fontSize=9)
#---------------------------------------------------------
plotInvBinaryTree!(markOfLeftMostLeaf, markOfRightMostLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=widthOfTree, heightOfTree=heightOfTree, fontSize=fontSize)
#---------------------------------------------------------
plotInvBinaryTree!(markOfLeftLeaf, markOfRightLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=widthOfTree/3, heightOfTree=heightOfTree, fontSize=fontSize)
end # function plotQuaternyTree!
#------------------------------------------------------------------------------
plot(xlim=(-1.5, 8.0), ylim=(0, yMax), legend=:false, ticks=:none)
plotInvBinaryTree!("1", "2", "•", 3.0, yMax-1.0; widthOfTree=4.0, heightOfTree=1)
#------------------------------------------------------------------------------
end # let
# ╔═╡ b068c8b6-7cfd-47c0-9e87-ee4cd7537bce
md"
**Fig. 2.1.1.3** $$consCell := (1 \bullet 2)$$
---
"
# ╔═╡ 3fba0329-19ac-485a-a56d-ee64ae0e3573
consCell = cons(1, 2) # construction of 2-tuple consCell
# ╔═╡ 44d3380a-b2ab-43e1-8f03-a14a9599fac0
typeof(consCell)
# ╔═╡ 44017d8b-476e-4e9c-abac-229e9a2e85d1
car(consCell) # selection of field 'car' of 2-tuple consCell
# ╔═╡ 51732672-ee53-46e7-85bf-217f531867cc
cdr(consCell) # selection of field 'cdr' of 2-tuple consCell
# ╔═╡ 6fa45a52-31fd-44c6-9928-4a75a7feccc0
let
yMax = 4
#------------------------------------------------------------------------------
function plotHorizontalLine!(markOfLeftNode, markOfRightNode, coordinateXOfMiddle, coordinateYOfMiddle; lengthOfLine=2.0, fontSize=9)
plot!([ # plot of horizontal bar '-'
(coordinateXOfMiddle-lengthOfLine/2+lengthOfLine/10, coordinateYOfMiddle),
(coordinateXOfMiddle+lengthOfLine/2-lengthOfLine/10, coordinateYOfMiddle)],
lw=1, linecolor=:black, arrow=true)
annotate!( # mark of left node 'x'
(coordinateXOfMiddle-lengthOfLine/2, coordinateYOfMiddle, text(markOfLeftNode, fontSize, :blue)))
annotate!( # mark of right node 'x'
(coordinateXOfMiddle+lengthOfLine/2, coordinateYOfMiddle, text(markOfRightNode, fontSize, :blue)))
end # function plotHorizontalLine!
#------------------------------------------------------------------------------
function plotInvUnaryTree!(markOfLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; heightOfTree=2, fontSize=9)
plot!([ # plot of middle vertical arm '|'
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree/8), # root
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree+heightOfTree/8)], # leaf
lw=1, linecolor=:black)
annotate!( # mark of leaf 'x'
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree, text(markOfLeaf, fontSize, :blue)))
annotate!( # mark of root 'x'
(coordinateXOfRootMark, coordinateYOfRootMark, text(markOfRoot, fontSize, :blue)))
end # function plotUnaryTree!
#------------------------------------------------------------------------------
function plotInvBinaryTree!(markOfLeftLeaf, markOfRightLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=1.5, heightOfTree=2, fontSize=9)
annotate!( # mark of left leaf 'x'
(coordinateXOfRootMark-widthOfTree/2,
coordinateYOfRootMark-heightOfTree,
text(markOfLeftLeaf, fontSize, :blue)))
plot!([ # plot of left vertical arm '|'
(coordinateXOfRootMark-widthOfTree/2,
coordinateYOfRootMark-heightOfTree+heightOfTree/8),
(coordinateXOfRootMark-widthOfTree/2, coordinateYOfRootMark-heightOfTree/2)],
lw=1, linecolor=:black)
annotate!( # mark of right leaf 'x'
(coordinateXOfRootMark+widthOfTree/2,
coordinateYOfRootMark-heightOfTree,
text(markOfRightLeaf, fontSize, :blue)))
plot!([ # plot of right vertical arm '|'
(coordinateXOfRootMark+widthOfTree/2,
coordinateYOfRootMark-heightOfTree+heightOfTree/8),
(coordinateXOfRootMark+widthOfTree/2, coordinateYOfRootMark-heightOfTree/2)],
lw=1, linecolor=:black)
plot!([ # plot of horizontal bar '-'
(coordinateXOfRootMark-widthOfTree/2, coordinateYOfRootMark-heightOfTree/2),
(coordinateXOfRootMark+widthOfTree/2, coordinateYOfRootMark-heightOfTree/2)],
lw=1, linecolor=:black)
plot!([ # plot of middle vertical arm '|'
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree/2),
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree/8)],
lw=1, linecolor=:black)
annotate!( # mark of root leaf 'x'
(coordinateXOfRootMark, coordinateYOfRootMark, text(markOfRoot, fontSize, :blue)))
end # function plotBinaryTree!
#------------------------------------------------------------------------------
function plotInvTernaryTree!(markOfLeftLeaf, markOfMiddleLeaf, markOfRightLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=3.0,
heightOfTree=2, fontSize=9)
#---------------------------------------------------------
plotInvBinaryTree!(markOfLeftLeaf, markOfRightLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=widthOfTree, heightOfTree=heightOfTree, fontSize=fontSize)
#---------------------------------------------------------
plotInvUnaryTree!(markOfMiddleLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; heightOfTree=heightOfTree, fontSize=fontSize)
end # function plotTernaryTree!
#------------------------------------------------------------------------------
function plotInvQuaternyTree!(markOfLeftMostLeaf, markOfLeftLeaf, markOfRightLeaf, markOfRightMostLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=4.0, heightOfTree=2, fontSize=9)
#---------------------------------------------------------
plotInvBinaryTree!(markOfLeftMostLeaf, markOfRightMostLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=widthOfTree, heightOfTree=heightOfTree, fontSize=fontSize)
#---------------------------------------------------------
plotInvBinaryTree!(markOfLeftLeaf, markOfRightLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=widthOfTree/3, heightOfTree=heightOfTree, fontSize=fontSize)
end # function plotQuaternyTree!
#------------------------------------------------------------------------------
plot(xlim=(-1.5, 8.0), ylim=(0, yMax), legend=:false, ticks=:none)
plotHorizontalLine!("y", "•", 2.0, yMax-2.0;lengthOfLine=4.0)
plotInvBinaryTree!("3", "4", "•", 4.0, yMax-2.0; widthOfTree=4.0, heightOfTree=1)
#------------------------------------------------------------------------------
end # let
# ╔═╡ 6a584664-cda8-41c1-8bfe-2b7d5b5e1e28
md"
**Fig. 2.1.1.4** consCell $$y \mapsto (3 \bullet 4)$$
---
"
# ╔═╡ a1587038-c11d-4b4b-8436-b098b61f1077
y = cons(3, 4) # construction of 2-tuple (= cons-cell) y
# ╔═╡ 118b16a3-093f-4b39-823b-ac167da69615
car(y)
# ╔═╡ fb4ecf01-1361-43c5-854d-8c83969a2e60
cdr(y)
# ╔═╡ 099f789b-9fdd-4643-8c6e-b5054af12736
let
yMax = 4
#------------------------------------------------------------------------------
function plotHorizontalArrowFromLeftToRight!(markOfLeftNode, markOfRightNode, coordinateXOfMiddle, coordinateYOfMiddle; lengthOfLine=2.0, fontSize=9)
plot!([ # plot of horizontal bar '-'
(coordinateXOfMiddle-lengthOfLine/2+lengthOfLine/10, coordinateYOfMiddle),
(coordinateXOfMiddle+lengthOfLine/2-lengthOfLine/10, coordinateYOfMiddle)],
lw=1, linecolor=:black, arrow=true)
annotate!( # mark of left node 'x'
(coordinateXOfMiddle-lengthOfLine/2, coordinateYOfMiddle, text(markOfLeftNode, fontSize, :blue)))
annotate!( # mark of right node 'x'
(coordinateXOfMiddle+lengthOfLine/2, coordinateYOfMiddle, text(markOfRightNode, fontSize, :blue)))
end # function plotHorizontalArrowFromLeftToRight!
#------------------------------------------------------------------------------
function plotHorizontalArrowFromRightToLeft!(markOfLeftNode, markOfRightNode, coordinateXOfMiddle, coordinateYOfMiddle; lengthOfLine=2.0, fontSize=9)
plot!([ # plot of horizontal bar '-'
(coordinateXOfMiddle+lengthOfLine/2-lengthOfLine/10, coordinateYOfMiddle),
(coordinateXOfMiddle-lengthOfLine/2+lengthOfLine/10, coordinateYOfMiddle)],
lw=1, linecolor=:black, arrow=true)
annotate!( # mark of left node 'x'
(coordinateXOfMiddle-lengthOfLine/2, coordinateYOfMiddle, text(markOfLeftNode, fontSize, :blue)))
annotate!( # mark of right node 'x'
(coordinateXOfMiddle+lengthOfLine/2, coordinateYOfMiddle, text(markOfRightNode, fontSize, :blue)))
end # function plotHorizontalArrowFromRightToLeft!
#------------------------------------------------------------------------------
function plotInvUnaryTree!(markOfLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; heightOfTree=2, fontSize=9)
plot!([ # plot of middle vertical arm '|'
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree/8), # root
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree+heightOfTree/8)], # leaf
lw=1, linecolor=:black)
annotate!( # mark of leaf 'x'
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree, text(markOfLeaf, fontSize, :blue)))
annotate!( # mark of root 'x'
(coordinateXOfRootMark, coordinateYOfRootMark, text(markOfRoot, fontSize, :blue)))
end # function plotUnaryTree!
#------------------------------------------------------------------------------
function plotInvBinaryTree!(markOfLeftLeaf, markOfRightLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=1.5, heightOfTree=2, fontSize=9)
annotate!( # mark of left leaf 'x'
(coordinateXOfRootMark-widthOfTree/2,
coordinateYOfRootMark-heightOfTree,
text(markOfLeftLeaf, fontSize, :blue)))
plot!([ # plot of left vertical arm '|'
(coordinateXOfRootMark-widthOfTree/2,
coordinateYOfRootMark-heightOfTree+heightOfTree/8),
(coordinateXOfRootMark-widthOfTree/2, coordinateYOfRootMark-heightOfTree/2)],
lw=1, linecolor=:black)
annotate!( # mark of right leaf 'x'
(coordinateXOfRootMark+widthOfTree/2,
coordinateYOfRootMark-heightOfTree,
text(markOfRightLeaf, fontSize, :blue)))
plot!([ # plot of right vertical arm '|'
(coordinateXOfRootMark+widthOfTree/2,
coordinateYOfRootMark-heightOfTree+heightOfTree/8),
(coordinateXOfRootMark+widthOfTree/2, coordinateYOfRootMark-heightOfTree/2)],
lw=1, linecolor=:black)
plot!([ # plot of horizontal bar '-'
(coordinateXOfRootMark-widthOfTree/2, coordinateYOfRootMark-heightOfTree/2),
(coordinateXOfRootMark+widthOfTree/2, coordinateYOfRootMark-heightOfTree/2)],
lw=1, linecolor=:black)
plot!([ # plot of middle vertical arm '|'
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree/2),
(coordinateXOfRootMark, coordinateYOfRootMark-heightOfTree/8)],
lw=1, linecolor=:black)
annotate!( # mark of root leaf 'x'
(coordinateXOfRootMark, coordinateYOfRootMark, text(markOfRoot, fontSize, :blue)))
end # function plotBinaryTree!
#------------------------------------------------------------------------------
function plotInvTernaryTree!(markOfLeftLeaf, markOfMiddleLeaf, markOfRightLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=3.0,
heightOfTree=2, fontSize=9)
#---------------------------------------------------------
plotInvBinaryTree!(markOfLeftLeaf, markOfRightLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=widthOfTree, heightOfTree=heightOfTree, fontSize=fontSize)
#---------------------------------------------------------
plotInvUnaryTree!(markOfMiddleLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; heightOfTree=heightOfTree, fontSize=fontSize)
end # function plotTernaryTree!
#------------------------------------------------------------------------------
function plotInvQuaternyTree!(markOfLeftMostLeaf, markOfLeftLeaf, markOfRightLeaf, markOfRightMostLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=4.0, heightOfTree=2, fontSize=9)
#---------------------------------------------------------
plotInvBinaryTree!(markOfLeftMostLeaf, markOfRightMostLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=widthOfTree, heightOfTree=heightOfTree, fontSize=fontSize)
#---------------------------------------------------------
plotInvBinaryTree!(markOfLeftLeaf, markOfRightLeaf, markOfRoot, coordinateXOfRootMark, coordinateYOfRootMark; widthOfTree=widthOfTree/3, heightOfTree=heightOfTree, fontSize=fontSize)
end # function plotQuaternyTree!
#------------------------------------------------------------------------------
plot(xlim=(-2.5, 12.0), ylim=(0, yMax), legend=:false, ticks=:none)
plotHorizontalArrowFromRightToLeft!("•", "y", 9.0, yMax-2.0;lengthOfLine=4.0)
plotHorizontalArrowFromLeftToRight!("x", "•", 1.0, yMax-2.0;lengthOfLine=4.0)
plotHorizontalArrowFromLeftToRight!("z", "•", 2.0, yMax-1.0;lengthOfLine=6.0)
plotInvBinaryTree!("3", "4", "•", 7.0, yMax-2.0; widthOfTree=2.0, heightOfTree=1)
plotInvBinaryTree!("1", "2", "•", 3.0, yMax-2.0; widthOfTree=2.0, heightOfTree=1)
plotInvBinaryTree!("•", "•", "•", 5.0, yMax-1.0; widthOfTree=4.0, heightOfTree=1)
#------------------------------------------------------------------------------
end # let
# ╔═╡ d1cd6289-c53d-4561-9895-4958c3e150b5
md"
**Fig. 2.1.1.5** consCell $$z \mapsto cons(x \bullet y) = ((1 \bullet 2) \bullet (3 \bullet 4))$$
---
"
# ╔═╡ f0c42132-4b04-4b48-aa45-b45b4f585466
x = cons(1, 2)
# ╔═╡ 132232a8-495f-4f2c-a08a-da4b898f606b
z = cons(x, y) # construction of 2-tuple z
# ╔═╡ 768b4543-6d34-4af7-9ee1-6120a33a169d
car(z)
# ╔═╡ 8b990c96-d5e2-4e77-ae73-eae3bc399f9d
cdr(z)
# ╔═╡ 4e7da88e-e73e-4d15-bd5f-b1eed925322d
car(car(z))
# ╔═╡ 9b7d9d52-77c6-4328-8018-3b1e40fb9558
car(cdr(z))
# ╔═╡ 7ce9706c-99ed-4dd8-bf14-19ad346cfbf4
md"
---
##### 2.1.1.1.3 Representing Rational Numbers (... with *Named* Tuples)
"
# ╔═╡ 8b2fb52c-7519-410f-a872-610cdb911b72
md"
##### 2.1.1.1.3.1 Abstract *Untyped* Constructor $$makeRat$$ based on $$cons$$
"
# ╔═╡ 680c5ec0-6cf2-4b0c-a005-751ef8a7e568
md"
###### *1st* method (default, *un*typed, *without* gcd) of function $$makeRat$$
"
# ╔═╡ 7ff3b4a0-e00b-424f-8118-897019d0fc20
makeRat(n, d) = cons(n, d)
# ╔═╡ 810b1eca-eac7-49cb-a34f-8328ea432824
md"
###### *2nd* method (*specialized*, *typed*, *with* gcd) of function $$makeRat$$
###### ... with type $$Signed$$ and imperative *looping* construct $$while$$
"
# ╔═╡ df37be85-589a-46aa-a14c-8ab67c461ae9
function makeRat(n::Signed, d::Signed)
#----------------------------------------------------------
function gcd2(a, b)
#------------------------------------------------------
remainder = % # local function definition
#------------------------------------------------------
b == 0 ? a : gcd(b, remainder(a, b))
#----------------------------------------=-------------
while !(b == 0)
a,b = b, remainder(a, b) # parallel (!) assignment
end # while
a
#----------------------------------------=-------------
end # function gcd2
#----------------------------------------=-----------------
let g = gcd2(n, d)
cons(n ÷ g, d ÷ g)
end # let
#----------------------------------------=-----------------
end # function makeRat
# ╔═╡ 764ec0e3-66c3-4a67-9473-95380e11250b
md"
##### 2.1.1.1.3.2 Abstract *Un*typed Selectors $$numer, denom$$ based on $$car, cdr$$
"
# ╔═╡ 193ae321-0f26-44cc-a48f-1a1b9bc71af8
numer(x) = car(x) # definition of abstract selector 'numer'
# ╔═╡ d5c0f2d2-7e6b-4fc5-b9a2-de927eb5c024
denom(x) = cdr(x) # definition of abstract selector 'denom'
# ╔═╡ ca75c0f7-2e85-4436-8544-9b19aa0f57a8
function addRat(x, y)
makeRat(+(*(numer(x), denom(y)), *(numer(y), denom(x))), *(denom(x), denom(y)))
end # function addRat
# ╔═╡ a181310f-1f46-43b2-8702-a8a60308ccfe
function subRat(x, y)
makeRat(-(*(numer(x), denom(y)), *(numer(y), denom(x))), *(denom(x), denom(y)))
end # function subRat
# ╔═╡ 778e12ad-bfe7-4f02-a946-894249fe2375
function mulRat(x, y)
makeRat(*(numer(x), numer(y)), *(denom(x), denom(y)))
end # function mulRat
# ╔═╡ dce13d9a-7ffd-475a-84de-7826a1198f38
function divRat(x, y)
makeRat(*(numer(x), denom(y)), *(denom(x), numer(y)))
end # function divRat
# ╔═╡ bc19acb5-0d08-4f7e-abce-c77fca0e8ac9
function equalRat(x, y)
*(numer(x), denom(y)) == *(denom(x), numer(y))
end # function equalRat
# ╔═╡ 61340177-3808-4a2b-906f-51b801178c6f
md"
##### 2.1.1.1.3.3 Output function $$print\_rat$$
(= Transformation of *internal* into *external* form)
"
# ╔═╡ 172db576-f756-4d62-94c3-128c6ac4f847
# idiomatic Julia-code with string interpolation "$(.....)"
printRat(x) = "$(numer(x))//$(denom(x))"
# ╔═╡ a9341f5b-8b06-4994-ba6b-58070485c336
md"
##### 2.1.1.1.4 Applications
"
# ╔═╡ 9b1a0332-0170-424b-be4d-abba0c042ff6
one_half = makeRat(1, 2)
# ╔═╡ 5e19e77d-ccea-4825-807d-5a278d762978
typeof(one_half)
# ╔═╡ cafada61-82c1-4231-bcd8-9887df2be87c
one_third = makeRat(1, 3)
# ╔═╡ e20d52b3-204c-4105-a362-2b9e66fe22f9
two_twelves = makeRat(2, 12) # with (!) application of gcd
# ╔═╡ 63d09ac2-193d-49dc-9eff-d5c14f973627
addRat(one_half, one_third) # 1/2 + 1/3 = 3/6 + 2/6 = 5/6
# ╔═╡ 69facaff-5c1f-4c8e-a436-81e8f9cd73b9
equalRat(makeRat(2, 3), makeRat(6, 9)) # 2/3 = 6/9
# ╔═╡ 45772e1e-aa83-46cd-add7-4f977f58dff0
equalRat(makeRat(1, 2), makeRat(3, 6)) # 1/2 = 3/6 => 1/2 = 1/2
# ╔═╡ d3d1980c-00f3-4d04-abe5-8705f61b3459
equalRat(makeRat(4, 3), makeRat(120, 90)) # 4/3 = 120/90 => 4/3 = 4/3
# ╔═╡ a861fc71-5a96-43b4-9a8f-94a603d2cb3b
md"
###### new: [rational approximation to π](https://en.wikipedia.org/wiki/Approximations_of_%CF%80) accurate to seven digits
"
# ╔═╡ aa7ea22e-f330-427e-9d89-408003e6b8f2
355/113
# ╔═╡ bf4034ad-07ff-4400-8aba-3b855fc43a5c
355/113 - π # deviation from Julia π
# ╔═╡ c70c4a99-32b9-4c70-8b36-064d313e753c
md"
---
#### 2.1.1.2 idiomatic *imperative*, *typed* Julia ...
###### ... with types $$Signed$$, $$Pair$$, $$Rational$$, *multiple* methods, '%', and 'while'
"
# ╔═╡ 285428ce-c13e-43ca-add3-672b1e454e18
md"
##### 2.1.1.2.1 Arithmetic Operations for Rational Numbers
###### *2nd* methods (*specialized*, *typed* ($$Pair$$)) of functions $$addRat$$, $$subRat$$, $$mulRat$$, $$divRat$$, $$equalRat$$
"
# ╔═╡ cc592f32-8785-4735-badd-de3903c20f05
md"
---
##### 2.1.1.2.2 Pairs (... as Julia's $$Pair$$)
###### Scheme's pair constructor $$cons$$ and Selectors $$car$$ and $$cdr$$ are implemented here as Julia's $$Pair$$
"
# ╔═╡ d4c053d6-ba2c-4d88-beef-19873926df88
md"
###### *2nd* method (*specialized*, *typed*) of function $$cons, car$$, and $$cdr$$
definition of constructor $$cons$$ with constructor $$Pair(<par1>, <par2>)$$ and fields $$first$$ and $$second$$. These selectors are deferred and hided into the *Scheme*-like selctors $$car, cdr$$.
"
# ╔═╡ 20fa866b-a9d2-4446-a57a-c01fe145aaac
md"
$$\begin {array}{c|c|c}
& \text{Abstraction Hierarchy} & \\
\hline
\text{ Abstract} & addRat2, subRat2 & \text{level 2} \\
\text{Operators} & mulRat2, divRat2, equalRat2 & \text{Domain} \\
& printRat & \\
\hline
\text{Constructor/} & makeRat2 & \text{level 1} \\
\text{Selectors} & numer2, denom2 & \\
\hline
\text{Constructor/} & cons2 & \text{level 0} \\
\text{Selectors} & car2, cdr2 & \textit{Scheme}\text{-like} \\
\hline
\text{Constructor/} & consCell = Pair(first:...,second:...) & \text{level -1} \\
\text{Selectors} & consCell.first, consCell.second & \textit{Julia} \\
\hline
\end {array}$$
**Fig. 2.1.1.6**: *Second* abstraction hierarchy for *rational* number algebra or [here](https://sarabander.github.io/sicp/html/2_002e1.xhtml#g_t2_002e1))
---
"
# ╔═╡ 0ffbaeab-9ff4-484b-95df-3b6aab526e0d
# idiomatic Julia-code by built-in types 'Signed' and constructor 'Pair'
cons2(car::Signed, cdr::Signed)::Pair = Pair(car::Signed, cdr::Signed)::Pair
# ╔═╡ 1fddd3ae-5da7-4a32-a53e-077b8f37509c
car2(cons::Pair)::Signed = cons.first::Signed # Pair-selector 'first'
# ╔═╡ b52a93ce-d551-4c8d-bcdc-2a5d69facdcc
cdr2(cons::Pair)::Signed = cons.second::Signed # Pair-selector 'second'
# ╔═╡ d84101ea-7172-4e1f-9929-1a23acd7a7c7
md"
---
##### 2.1.1.2.3 Representing Rational Numbers (... with type $$Pair$$)
"
# ╔═╡ b4c9a74b-5a49-452e-bbf0-44826cd92e46
md"
###### 2.1.1.2.3.1 Abstract *Typed* Constructor $$make\_rat$$
"
# ╔═╡ 3498e844-931f-4d46-a9aa-b9a2d3b892e7
# idiomatic Julia-code by '÷'
function makeRat2(n::Signed, d::Signed)::Pair
#----------------------------------------------------------
function gcd2(a::Signed, b::Signed)::Signed
#---------------------------------------
remainder = %
#---------------------------------------
b == 0 ? a : gcd(b, remainder(a, b))
#---------------------------------------
while !(b == 0)
a,b = b, remainder(a, b) # multiple (!) assignment
end # while
a
end # function gcd2
#----------------------------------------------------------
let g = gcd2(n, d)
cons2(n ÷ g, d ÷ g)::Pair
end # let
end # function makeRat2
# ╔═╡ 4e179b9f-021e-4891-8e27-7fa36b827fcf
md"
###### 2.1.1.2.3.2 Abstract *Typed* Selectors $$numer, denom$$
"
# ╔═╡ 7f5b9164-07c2-4ae1-888d-f601fa9d286c
numer2(x::Pair)::Signed = car2(x::Pair)::Signed
# ╔═╡ 3ca05b52-413f-46aa-9fd7-659227dccbd7
denom2(x::Pair)::Signed = cdr2(x::Pair)::Signed
# ╔═╡ 76e3a4bc-fde2-4440-96c1-c492cc120db0
function addRat2(x::Pair, y::Pair)::Pair
makeRat2(numer2(x) * denom2(y) + numer2(y) * denom2(x), denom2(x) * denom2(y))
end
# ╔═╡ 402d8963-26bc-4db2-b94b-ba4334e1d8fd
function subRat2(x::Pair, y::Pair)::Pair
makeRat2(numer2(x) * denom2(y) - numer2(y) * denom2(x), denom2(x) * denom2(y))
end
# ╔═╡ 09901068-bf38-4392-ab19-66a44d65344d
function mulRat2(x::Pair, y::Pair)::Pair
makeRat2(numer2(x) * numer2(y), denom2(x) * denom2(y))
end
# ╔═╡ 555e6212-22bd-4d0e-b221-edfe32f043f5
function divRat2(x::Pair, y::Pair)::Pair
makeRat2(numer2(x) * denom2(y), denom2(x) * numer2(y))
end
# ╔═╡ afe55885-aba6-4cbc-af2c-23d15bbbf6f5
function equalRat2(x::Pair, y::Pair)::Bool
numer2(x) * denom2(y) == denom2(x) * numer2(y)
end
# ╔═╡ bbccaeb3-48cf-4f65-9aa2-d0de9940e311
md"
###### 2.1.1.2.3.3 Output (= Transformation of *internal* into *external* form)
"
# ╔═╡ 1248e659-dcdb-442c-859c-3289d5561c6f
printRat(x::Pair)::String = "$(numer2(x))//$(denom2(x))"
# ╔═╡ edd94612-1772-4c7a-b8d5-cf5b4a540487
md"
###### 2.1.1.2.4 Applications
"
# ╔═╡ 0e8fd444-fd9d-4455-8ca2-9d61f84e74d9
a2 = cons2(2, 3)
# ╔═╡ 32e3fc67-3872-4a28-bd6b-9cf06bc094ac
car2(a2)
# ╔═╡ e5fbf0c8-1123-4e4c-a5cd-3aeff74d8231
cdr2(a2)
# ╔═╡ cc5f1bf0-f1ae-44fa-94af-e06fedae12f0
typeof(1.2)
# ╔═╡ 4bed1729-8c79-4cf1-95e3-3285010a5622
b2 = cons2(1.2, 3) # first argument is 'Float' but should be 'Signed'
# ╔═╡ 4ee29c75-f689-4332-99b9-d7b0951d4487
one_half2 = makeRat2(1, 2)
# ╔═╡ 86b40359-a87d-477e-9615-6b4803fe4fd7
one_third2 = makeRat2(1, 3)
# ╔═╡ d650d480-08ed-478a-8a9f-92cce23ba072
equalRat2(makeRat2(1, 3), makeRat2(2, 6))
# ╔═╡ 999406e8-787a-4f2b-9805-eed631fc6f54
equalRat2(makeRat2(1, 2), makeRat2(3, 6))
# ╔═╡ 4bbd2664-5846-4487-b91c-be7ff75a4e81
equalRat2(makeRat2(1, 3), makeRat2(3, 6))
# ╔═╡ 5be39e55-32ba-4809-9456-431e118b9195
md"
---
#### 2.1.1.3 idiomatic *imperative*, *typed* Julia ...
###### ... with type $$Rational$$
"
# ╔═╡ bb838586-2f8a-44f9-9672-aced83e8cf76
md"
##### 2.1.1.3.1 Arithmetic Operations for Rational Numbers
###### *3rd* methods (*specialized*, *typed* ($$Rational$$)) of functions $$add\_rat$$, $$sub\_rat$$, $$mul\_rat$$, $$div\_rat$$, $$equal\_rat$$
"
# ╔═╡ 26593de4-f70d-4aa5-86dd-d09bb6544407
function addRat3(x::Rational, y::Rational)::Rational
x + y
end # function addRat3
# ╔═╡ 6662d041-af9c-472d-b9fe-efe2c4846267
function subRat3(x::Rational, y::Rational)::Rational
x - y
end # function subRat3
# ╔═╡ 8fc62f98-9a2e-4fae-83b7-a78118b656d3
function mulRat3(x::Rational, y::Rational)::Rational
x * y
end # function mulRat3
# ╔═╡ f7bd0981-1534-4c80-9697-e019f29c4a3c
function divRat3(x::Rational, y::Rational)::Rational
x // y
end # function divRat3
# ╔═╡ b25d2465-7452-43e8-b709-06009642076a
function equalRat3(x::Rational, y::Rational)::Bool
x == y
end # function equalRat3
# ╔═╡ 32927d21-4f21-41ff-b90c-21a9c15a7a26
md"
---
##### 2.1.1.3.2 Pairs (... as Julia's $$Rational$$)
###### Scheme's pair constructor $$cons$$ and Selectors $$car$$ and $$cdr$$ are implemented here as Julia's $$Rational$$
"
# ╔═╡ 5f1971c7-f3bb-4dbf-aa42-4218055e1fd0
md"
###### *3rd* method (*specialized*, *typed*) of function $$cons, car$$, and $$cdr$$
definition of constructor $$cons$$ with constructor $$//$$ and fields $$numerator$$ and $$denominator$$
"
# ╔═╡ 61794b44-f955-4486-8b1e-863dca1082c2
md"
$$\begin {array}{c|c|c}
& \text{Abstraction Hierarchy} & \\
\hline
\text{ Abstract} & addRat3, subRat3 & \text{level 2} \\
\text{Operators} & mulRat3, divRat3, equalRat3 & \text{Domain} \\
& printRat & \\
\hline
\text{Constructor/} & makeRat3 & \text{level 1} \\
\text{Selectors} & numer3, denom3 & \\
\hline
\text{Constructor/} & cons3 & \text{level 0} \\
\text{Selectors} & car3, cdr3 & \textit{Scheme}\text{-like} \\
\hline
\text{Constructor/} & consCell = car // cdr & \text{level -1} \\
\text{Selectors} & numerator(consCell), denominator(consCell) & \textit{Julia} \\
\hline
\end {array}$$
**Fig. 2.1.1.7**: *Third* abstraction hierarchy for *rational* number algebra or [here](https://sarabander.github.io/sicp/html/2_002e1.xhtml#g_t2_002e1))
---
"
# ╔═╡ 47d605b7-ec32-4b72-a329-bf47e7273972
cons3(car::Signed, cdr::Signed)::Rational = car // cdr
# ╔═╡ e6e6754c-d86e-449f-a82e-03e0ab2a91cb
car3(cons::Rational)::Signed = numerator(cons)::Signed
# ╔═╡ c356aab4-4717-46a7-b041-aa14800a0253
cdr3(cons::Rational)::Signed = denominator(cons)::Signed
# ╔═╡ abf821d6-3f8a-404d-903d-0a4b49ed9d9b
md"
---
##### 2.1.1.3.3 Representing Rational Numbers (... with type $$Rational$$)
"
# ╔═╡ ce97fbfe-0a85-43c6-980f-18da5e795a98
md"
###### 2.1.1.3.3.1 Abstract *Typed* Constructor $$make\_rat3$$
"
# ╔═╡ 6858b169-b9dc-4a23-8dd3-8951033bd311
# idiomatic Julia-code by '÷'
function makeRat3(n::Int, d::Int)::Rational
n//d
end # function makeRat3
# ╔═╡ 8d014b03-6124-4937-aa35-599c01301efd
md"
###### 2.1.1.3.3.2 Abstract *Typed* Selectors for Type $$Rational$$
"
# ╔═╡ 0a935144-2c44-480b-9c6e-53408b972e89
numer3(x::Rational)::Signed = numerator(x::Rational)::Signed
# ╔═╡ 7c2af484-eea8-4c8c-8891-4aa4923298ae
denom3(x::Rational)::Signed = denominator(x::Rational)::Signed
# ╔═╡ 846b7522-8088-49b4-8848-9f0031e1004e
md"
##### 2.1.1.3.4 Applications
"
# ╔═╡ bb81d86d-63f6-4e9b-83ba-f10a6fc386b0
cons3(1, 3)
# ╔═╡ 96d4be2d-fab7-484e-8a1e-837df8e71574
car3(cons3(1, 3))
# ╔═╡ d07fe6c8-8577-4ece-94d9-c61d44bc502c
cdr3(cons3(1, 3))
# ╔═╡ 6d096e3e-3313-4889-aa5b-387e75912d5e
printRat(x::Rational)::String = "$(numer3(x))//$(denom3(x))"
# ╔═╡ 8bba74f5-f038-448e-9459-8b6a945c293d
methods(printRat)
# ╔═╡ 6931655c-b8bc-4efb-bad1-5061a5b63548
printRat(one_half)
# ╔═╡ 1d2a90d7-cf23-4cef-b525-f0e0cb77586f
printRat(one_third)
# ╔═╡ c85ab30f-22b4-452d-adb0-401dd0609d79
printRat(two_twelves) # with (!) application of gcd
# ╔═╡ 64d8fdee-f7b2-453d-b595-f3d27c69ed20
printRat(makeRat(120,90)) # 120/90 = 12/9 = 4/3
# ╔═╡ 60367294-bac2-4c34-bddd-fc8c8c3a2f34
printRat(addRat(one_half, one_third)) # 1/2 + 1/3 = 3/6 + 2/6 = 5/6
# ╔═╡ 6009d1d7-f87d-440e-bfd5-389f7a6189fe
printRat(subRat(one_half, one_third)) # 1/2 - 1/3 = 3/6 - 2/6 = 1/6
# ╔═╡ d2c09e98-d6b6-43e0-a493-a5c00700b022
printRat(mulRat(one_half, one_third)) # 1/2 * 1/3 = 1/6
# ╔═╡ e8dade6c-874e-4926-bdca-0875509f35ff
printRat(divRat(one_half, one_third)) # (1/2)/(1/3) = (1*3)/(2*1) = 3/2
# ╔═╡ ab78e9ac-762d-4824-a17a-b61695010615
printRat(addRat(one_third, one_third)) # 6/9 = 2/3
# ╔═╡ 5e3ce89e-16cf-41ab-b65b-0693f152d2e6
printRat(makeRat(355,113))
# ╔═╡ 97c12566-f149-46c0-bb00-55686029f523
methods(printRat)
# ╔═╡ 451f4fcd-3cb6-400b-b7eb-b80cb6a5a712
printRat(one_half2)
# ╔═╡ db489898-bef8-4562-adf7-fab1f56e566e
printRat(one_third2)
# ╔═╡ 02fe4860-7fd6-453a-8eb2-a4654d7dd77b
printRat(addRat2(one_half2, one_third2))
# ╔═╡ 04a336e5-b3d0-4c0c-9275-1ad23a1f19e2
printRat(subRat2(one_half2, one_third2))
# ╔═╡ ed2ac17e-497e-4b96-ba9b-ae7d4433c731
printRat(mulRat2(one_half2, one_third2))
# ╔═╡ 55360263-ea18-4987-98c6-396a88afc60e
printRat(divRat2(one_half2, one_third2))
# ╔═╡ 6598342e-f99c-486f-b486-69ee6d488f75
printRat(addRat2(one_third2, one_third2))
# ╔═╡ 985720fb-c4a2-45cd-9094-6d9363deeca1
printRat(makeRat2(120,90))
# ╔═╡ cbb3d0bd-bf78-4f6b-b332-2289393697ec
methods(printRat)
# ╔═╡ 9672253e-c0be-4da5-9c1b-6882ce5f65c3
one_half3 = makeRat3(1, 2)
# ╔═╡ ed140d1c-110e-4fcb-ba43-807b59ac30ef
printRat(one_half3)
# ╔═╡ ea6f8db4-c507-44d3-9e29-6bfc6b85b4ad
one_third3 = makeRat3(1, 3)
# ╔═╡ ed389d11-5f6c-496c-84bf-6ac6b05ff5a4
printRat(one_third3)
# ╔═╡ dc14d933-62ac-4cf7-8563-71fda97afc9a
printRat(addRat3(one_half3, one_third3))
# ╔═╡ 47771613-e613-497b-a020-f825a51b012d