-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCM20210822_SICP_2.2.1_RepresentingSequences.jl
1141 lines (913 loc) · 35.8 KB
/
PCM20210822_SICP_2.2.1_RepresentingSequences.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
# ╔═╡ 373a14a0-036b-11ec-3063-73539b537973
md"
======================================================================================
#### SICP: [2.2.1 Representing Sequences](https://sarabander.github.io/sicp/html/2_002e2.xhtml#g_t2_002e2_002e1)
###### file: PCM20210822\_SICP\_2.2.1\_RepresentingSequences.jl
###### Julia/Pluto.jl-code (1.8.5/...) by PCM *** 2023/03/01 ***
======================================================================================
"
# ╔═╡ 278e3d83-0583-459a-adec-95843b3314fd
md"
##### 2.2.1.1 SICP-Scheme-like *functional* Julia
"
# ╔═╡ 15d15f7e-ae79-4f36-87ca-0363ea9f598e
md"
---
##### Introduction
"
# ╔═╡ b2eb614e-8369-40f1-a7df-2551a3e20c9d
md"
###### 1st (default) untyped *method* of Scheme-like *constructor* function $$cons$$ implemented as Julia's $$namedTuple$$
"
# ╔═╡ ecbb360b-42d8-4ee8-8356-2028b1e5ccba
# (default) *untyped* constructor 'cons' with named fields 'car' and 'cdr'
cons(car, cdr) = (car=car, cdr=cdr)
# ^--------^------------- named fields
# ^-----^----------^--------^--------- parameter
# ╔═╡ 76b9bc7c-8afd-4e1d-a727-7646f377b09d
md"
###### 1st (default) *untyped* method of Scheme-like *selector* function $$car$$
"
# ╔═╡ f7000508-5beb-4d1f-8d9f-ca0dd98d94af
car(cons) = cons.car # definition of selector 'car'
# ^--------------------------- function name
# ^-------^--------------- parameter
# ^----------- field name
# ╔═╡ 100ac0a5-9f7d-4563-9fac-03967cb82e17
md"
###### 1st (default) *untyped* method of Scheme-like *selector* function $$cdr$$
"
# ╔═╡ e4bc1b01-7e3f-41ad-b054-e52912c9357c
cdr(cons) = cons.cdr # definition of selector 'cdr'
# ^--------------------------- function name
# ^-------^--------------- parameter
# ^----------- field name
# ╔═╡ 5a6d3aa0-cd51-4c8a-a5cd-4cb1082b5135
md"""
---
+-----+-----+ +-------+
----->| o | o--|----->| "two" |
+--|--+-----+ +-------+
|
|
+---+--+
| :one |
+------+
**Fig 2.2.1**: *Latent* box-and-pointer structure generated by $$cons(:one,\text{"two"})$$ (cf. Fig. 2.2, SICP, 1996)
---
"""
# ╔═╡ b4ef7e20-499d-4019-a694-f5e88c3e0d67
md"""
---
+-----+-----+ +-----+-----+
----->| o | o--|----->| o | o |
+--|--+-----+ +--|--+--|--+
| | |
+-----+-----+ +-----+ +-----+
| o | o | | 3 | | 4.0 |
+--|--+--|--+ +-----+ +-----+
| |
+------+ +-------+
| :one | | "two" |
+------+ +-------+
**Fig 2.2.2**: *Latent* box-and-pointer structure generated by $$cons(cons(:one, \text{"two"}), cons(3, 4.0))$$ (s.a. Fig. 2.3.left, SICP, 1996)
---
"""
# ╔═╡ 49d36791-24d6-4c7e-bec6-7593e8889478
md"""
---
+-----+-----+ +-------+
----->| o | o--|----->| 4.0 |
+--|--+-----+ +-------+
|
+-----+-----+ +-----+-----+
| o | o--|------->| o | o |
+--|--+-----+ +--|--+--|--+
| | |
+------+ +-------+ +-------+
| :one | | "two" | | 3 |
+------+ +-------+ +-------+
**Fig 2.2.3** *Latent* box-and-pointer structure generated by $$cons(cons(:one, cons(\text{"two"}, 3)), 4.0)$$ (s.a. Fig. 2.3.right, SICP, 1996)
---
"""
# ╔═╡ 4f343982-2aba-40ff-9d9d-f630ed4673f7
md"""
---
+-----+-----+ +-----+-----+ +-----+-----+ +-----+-----+
----->| o | o--|----->| o | o--|----->| o | o--|----->| o | / |
+--|--+-----+ +--|--+-----+ +--|--+-----+ +--|--+-----+
| | | |
+------+ +-------+ +-----+ +-----+
| :one | | "two" | | 3 | | 4.0 |
+------+ +-------+ +-----+ +-----+
**Fig 2.2.4**: The *latent* box-and-pointer structure of a *chain* (= *list*) of pairs generated by $$cons(:one, cons(\text{ "two" },cons(3, cons(4.0, :nil))))$$ (cf. Fig. 2.4, SICP, 1996)
---
"""
# ╔═╡ 04a3c30a-ccce-469c-9fa5-2b451082be8a
md"
###### function $$pp$$ ($$pp$$ = *pretty print*)
converts a *latent* box-pointer structure in a *manifest* *list* representation with parentheses $$(...)$$
"
# ╔═╡ b491dcdb-e51b-4438-8a26-e975e2a50533
md"
###### Testing $pp$
"
# ╔═╡ 47ff8a5c-4682-48c7-8080-272a73f38c3d
md"
---
##### Sequences
"
# ╔═╡ 54b14609-6552-40e9-9410-cbf8b8694a04
md"
###### 1st (default) untyped *method* of *constructor* function 'list' ...
###### ... *conceptualization* as *latent* box-pointer structure (=cons-cells) and *implementation* as Julia's $$NamedTuple$$
"
# ╔═╡ 531f20b7-7205-4c84-a0eb-63ddddc41180
md"
*(<par>...)* means *slurping*: *many* arguments are slurped into *one* parameter
*(<arg>...)* means *splatting*: *one* argument is splatted into *many* parameters
"
# ╔═╡ 79733f7a-fc76-4dff-9f2c-5774b334ce36
md"""
---
one_through_four
|
|
| +-----+-----+ +-----+-----+ +-----+-----+ +-----+-----+
+---->| o | o--|----->| o | o--|----->| o | o--|----->| o | / |
+--|--+-----+ +--|--+-----+ +--|--+-----+ +--|--+-----+
| | | |
+------+ +-------+ +-----+ +-----+
| :one | | "two" | | 3 | | 4.0 |
+------+ +-------+ +-----+ +-----+
**Fig. 2.2.5** *sequence* or *list* $$one\_through\_four$$ = $$list(:one, \text{"two"}, 3, 4.0)$$ (cf. Fig.2.4, SICP, 1996)
---
"""
# ╔═╡ 702449f7-20c2-43c9-a9a0-5be31ce61c4f
md"""
---
+---+---+ +---+---+ +---+---+ +---+---+ +---+---+
myList--->| o | o-|--->| o | o-|--->| o | o-|--->| o | o-|--->| o | / |
+---+---+ +---+---+ +---+---+ +---+---+ +---+---+
| | | | |
+-----+ +-----+ +-----+ +-----+ +-----+
| a | | b | | 3 | | 4 | | foo |
+-----+ +-----+ +-----+ +-----+ +-----+
**Fig. 2.2.6** Different types of elements in myList = list(:a, :b, 3, 4, "foo")
---
"""
# ╔═╡ a5ba5446-2f0e-4bba-8b36-77bcffd83c0e
md"
###### 1st (default) *untyped* method of combined *selector* function $$cadr$$ ...
"
# ╔═╡ 1428e283-676a-4553-8987-25eeb97d904a
md"
---
##### List operations
"
# ╔═╡ 7ac3bbee-5b12-4f0e-b2ef-b7244fb793fc
md"
###### 1st (default) untyped *method* of function $$list\_ref$$
"
# ╔═╡ af79cdc3-a4bf-4343-9371-9258da74be2b
md"
for $$n=0$$, $$list\_ref$$ should be the $$car$$ of the list
"
# ╔═╡ 20a4cca9-dd1b-4f60-82ef-ad50d13de35c
md"
###### 1st (default) *untyped* method of function $$length$$
"
# ╔═╡ a802dec0-ed35-468d-b4e2-a36a6405ec0d
md"
###### 2nd untyped *linear-recursive* variant method of function $$length$$
"
# ╔═╡ 49338ccd-1767-4fac-b7db-3ba7e04492ee
md"
###### 1st (default) *untyped* method of function $$append$$
"
# ╔═╡ 4ae3bca1-94a5-4464-92d8-1b76e710a652
md"
---
##### Mapping over lists
"
# ╔═╡ 5b9d814c-0878-44ef-913e-00cc89ad4639
md"
###### 1st (default) *untyped* method of function $$scale\_list$$
"
# ╔═╡ 005c006a-697c-4f6f-9f18-78c99af79e7b
md"
###### 1st (default) *untyped* method of function $$myMap$$
"
# ╔═╡ 8a462e3e-cbfb-47bc-8f4d-7f147c4e2cdd
md"
---
##### 2.2.1.2 Idiomatic *imperative* or *typed* Julia
"
# ╔═╡ 31576ae3-a89a-413a-92cd-6fe1eaba2d08
md"
###### 2nd (specialized) *typed variant* method of function $$list$$ ...
- implemented as Julia *Array* and ...
- the $$xs$$ parameter in $$list2$$ is *slurping* and ...
- the $$xs$$ argument in $$[xs::Any...]$$ is *splatting*
"
# ╔═╡ 3cd07141-0ed6-485b-8cd5-40aa615b5d8e
list2(xs::Any...)::Array = [xs::Any...]::Array # slurping (left) = spitting (right)
# ╔═╡ 9dc33f4f-33a3-4855-b0a2-f908a163ebf7
md"
---
+------->+---->+---->+---->+
| | | | |
one_through_four2 --->| +-----+-----+-----+-----+
+---->| 1 | 2 | 3 | 4 |
+-----+-----+-----+-----+
Fig. 2.2.7 *sequence* or *list* $$one\_through\_four2$$ = $$list2(1,2,3,4)$$ implemented as an Julia *array*
---
"
# ╔═╡ 224d8870-bb45-4ace-86eb-200e1a7868f9
one_through_four2 = list2(1,2,3,4)
# ╔═╡ 966d3379-43f3-45f2-b90f-b69739f0a53e
Tuple(one_through_four2)
# ╔═╡ 41a0e04c-d9ec-465c-922c-dca7e37d13a0
typeof(one_through_four2)
# ╔═╡ eefc868f-a339-4499-b450-e1dfbbbed481
md"""
---
+------->+---->+---->+---->+---->+
| | | | | |
myList2 --->| +-----+-----+-----+-----+-----+
+---->| :a | :b | 3 | 4 |"foo"|
+-----+-----+-----+-----+-----+
Fig. 2.2.8 $$myList2 = list2(:a, :b, 3, 4, \text{"foo"})$$ implemented as a Julia *array*
---
"""
# ╔═╡ 1d4cf0a0-187d-4c62-8589-0184d21e8ee1
myList2 = list2(:a, :b, 3, 4, "foo")
# ╔═╡ bf9ff18e-4ce0-405e-958b-d521f463a4c8
Tuple(myList2)
# ╔═╡ de40c17b-3fce-4abf-ba77-df817ae9dedf
typeof(myList2)
# ╔═╡ a5bda10e-20e7-4cd9-877a-f392a8c1c44b
md"
###### 2nd (specialized) *typed* method of *constructor* function $$cons$$ ...
###### ... implemented with Julia's *Array*
"
# ╔═╡ c1645190-174d-424a-abbd-c2abb488b7ba
# definition of a second variant for function 'cons'
cons(car::Any, list::Array)::Array = pushfirst!(list[1:end], car)
# ╔═╡ 9abffca7-23c0-4287-8b54-f69d1f95a19f
cons(:one, "two") # similar to SICP, 1996, Fig 2.2
# ╔═╡ e980b7d8-1c8b-4354-9bbd-313539b5c6ec
# Fig 2.2.2, similar to SICP, 1996, Fig. 2.3.left
cons(
cons(:one, "two"),
cons(3, 4.0))
# ╔═╡ 8203d83e-9ba8-40c2-8fb1-1f0f1f4a508c
# Fig 2.2.3, similar to SICP, 1996, Fig. 2.3.right
cons(
cons(1,
cons(2, 3)),
4)
# ╔═╡ 77a6bc9b-6948-40f7-85f4-862e1824d436
# Fig 2.2.4, similar to SICP, 1996, Fig. 2.4
cons(:one,
cons("two",
cons(3,
cons(4.0, :nil))))
# ╔═╡ b5d38b3c-8033-4898-af30-e385cb24cdd8
typeof(cons(:nil, :nil)) # ==> NamedTuple{(:car, :cdr), Tuple{Symbol, Symbol}}
# ╔═╡ 3c1528ac-63ed-4944-9afb-6e10b9b26793
typeof(cons(:nil, :nil)) <: NamedTuple
# ╔═╡ 1627b84f-d538-4d11-bd1e-2e2f3082e779
typeof(cons(:nil, :nil)) <: Tuple # ==> false --> :(
# ╔═╡ df60c1a7-3f91-4370-8232-135bfff959d1
cons(:one, # latent box-pointer structure of Fig. 2.2.4
cons("two",
cons(3,
cons(4.0, :nil))))
# ╔═╡ 413f608d-5bf5-4f93-9ef0-a9d6d7f8583b
list(elements...) = # parameter 'elements' slurps arguments
if ==(elements, ())
cons(:nil, :nil)
elseif ==(lastindex(elements), 1)
cons(elements[1], :nil)
else # 'elements[2:end]...' splatts its elements
cons(elements[1], list(elements[2:end]...))
end #if
# ╔═╡ be432d3e-6f08-4344-bee1-f2eeb9718fb4
list((:a, :b)) # ==> NamedTuple
# ╔═╡ 19dfb768-a350-4e70-a6fe-0d1f8eae96c4
one_through_four = # nested named tuples
list(:one, "two", 3, 4.0)
# ╔═╡ 1efc3f69-28f7-46fb-ba00-2dc1c434234b
myList = list(:a, :b, 3, 4, "foo") # nested named tuples
# ╔═╡ 0bf35cab-4249-4984-be04-cffb3aab352d
squares = list(1, 4, 9, 16, 25) # nested named tuples
# ╔═╡ b5fb717e-2988-4492-aa46-ac835b2099b5
odds = list(1, 3, 5, 7)
# ╔═╡ 606fa931-1420-4df3-b0c2-c903d161fb6b
cons(10, one_through_four) # nested named tuples
# ╔═╡ 88b6b567-3fc1-4af1-aecb-f1f1c0c1e75f
cons(5, one_through_four) # nested named tuples
# ╔═╡ 74d753ce-50a6-4075-9108-eec2aca841c1
md"
###### 2nd (specialized) *typed* method of *selector* function $$car$$
"
# ╔═╡ 8812f55d-1d06-49bd-a967-24077b720a39
car(list::Array)::Any = first(list::Array)
# ╔═╡ 3049ab44-f2a2-4baf-9765-301392337c3f
car(one_through_four)
# ╔═╡ efb2cdf0-b929-4b91-8ebf-22df244e577c
car(myList)
# ╔═╡ 3c6644b3-003a-44d1-b8f7-bc41edd467b3
md"
###### 2nd (specialized) *typed* method of *selector* function $$cdr$$
"
# ╔═╡ a66e8fe3-9f44-4d02-9e21-439cca73cd89
cdr(list::Array)::Array = list[2:end]::Array
# ╔═╡ 6f28288b-0cd9-4d32-b80b-1145a582a562
# iterative pp a box-pointer structure as tuple
function pp(consStruct::NamedTuple)::Tuple
#--------------------------------------------------------------------------------
function pp_iter(ppArray, consStruct::NamedTuple)
if consStruct == cons(:nil, :nil)
(Tuple(ppArray),)
elseif (car(consStruct) !== :nil) && (cdr(consStruct) == :nil)
Tuple(push!(ppArray, car(consStruct)))
else
pp_iter(push!(ppArray, car(consStruct)), cdr(consStruct))
end # if
end # pp_iter
#-----------------------------------------------------------------
pp_iter([], consStruct)
end # function pp
# ╔═╡ 78829665-5360-4c7a-ae09-b664560e4749
pp(cons(:one, "two")) # ==> dotted pair ==> *no* list structure ! *no* pp !
# ╔═╡ d404efa1-dc5f-40bb-8f10-bfb93dbd09ed
pp(cons(:one, cons("two", :nil)))
# ╔═╡ 25ae5618-cbc6-4325-bfb9-7be558102b9e
pp(cons(:nil, :nil)) # ==> (:nil) ==> (()) because :nil == ()
# ╔═╡ 04de1c1f-7c2c-4a07-89ad-f7fadc11d04a
typeof(pp(cons(:nil, :nil)))
# ╔═╡ cb1bd172-6e08-42d2-beb1-7278e5961f83
pp(cons(:a, :nil)) # ==> (:a, ()) ==> (:a) --> :)
# ╔═╡ 165c5ded-6f9f-42cd-9ece-d54a40e847e3
pp(cons(:a, cons(:b, :nil))) # ==> cons(:a, (:b)) ==> (:a, :b)
# ╔═╡ 5cec6d4d-4124-46a4-9400-d3c7bfeaa2b2
pp(cons(:one, # Scheme-like list of Fig. 2.2.4
cons("two",
cons(3,
cons(4.0, :nil)))))
# ╔═╡ d0518c8e-12b0-45c8-aae6-cbd5cdd82e00
pp(list()) # the empty list
# ╔═╡ 44db31bc-5b6b-4809-9464-97e09b1160cf
pp(one_through_four) # Scheme-like list
# ╔═╡ dee2d0c4-3431-4355-9c02-c15b8e335256
pp(cons(10, one_through_four)) # Scheme-like list
# ╔═╡ 143b00c0-8418-4ca2-b840-403bd7ca5faf
pp(cons(5, one_through_four)) # Scheme-like list
# ╔═╡ e0181f07-538d-489c-81bb-6ac2c779989e
pp(myList) # Scheme-like list
# ╔═╡ e13349e0-3a7a-4245-b6f7-5b8ea019053a
pp(squares) # # Scheme-like list
# ╔═╡ 52391fbc-87ac-42b1-8790-8dda0d3e1008
pp(odds)
# ╔═╡ be65d3f7-84cf-4d9d-9f8e-171f09745d23
cdr(one_through_four) # nested named tuples
# ╔═╡ 062906e4-1e1c-4d3d-8d18-ba3689c82b67
pp(cdr(one_through_four)) # Scheme-like list
# ╔═╡ 8c47ee7a-7bb5-44e3-aeb1-12f2f9f368eb
car(cdr(one_through_four))
# ╔═╡ 4439e6b7-27c1-4a8f-adc9-9de9b4efcc82
cdr(myList) # nested named tuples
# ╔═╡ 18a9bb82-0943-4fad-844f-da3878efb4bc
pp(cdr(myList)) # Scheme-like list
# ╔═╡ 2b2963a1-9db2-46f4-89b4-26b502cfc9a7
cadr(consCell) = car(cdr(consCell))
# ╔═╡ 95836205-128f-489e-b767-303adf325916
# according SICP first element should have index == 0
function list_ref(items, n)
if ==(n, 0)
car(items)
else
list_ref(cdr(items), -(n, 1))
end
end
# ╔═╡ c5c02c4f-6676-4a73-92a5-0d8a4c49330f
function length(list)
#--------------------------------------------------------------
null(list) = (car(list) == :nil) && (cdr(list) == :nil)
singleItem(list) = (car(list) !== :nil) && (cdr(list) == :nil)
#--------------------------------------------------------------
if null(list)
0
elseif singleItem(list)
1
else
+(1, length(cdr(list)))
end # if
end
# ╔═╡ 550ec189-2017-4489-b301-865986b6a8cf
function length2(list)
#--------------------------------------------------------------
null(list) = (car(list) == :nil) && (cdr(list) == :nil)
singleItem(list) = (car(list) !== :nil) && (cdr(list) == :nil)
#--------------------------------------------------------------
function length_iter(temp_list, count)
if null(temp_list)
count
elseif singleItem(temp_list)
count + 1
else
length_iter(cdr(temp_list), +(1, count))
end # if
end
#--------------------------------------------------------------
length_iter(list, 0)
end
# ╔═╡ 9e453d79-4c54-4c6c-bf4d-7b982fed5a18
length2(list())
# ╔═╡ c234ab19-8fa7-4963-bd88-3077c3924502
length2(list(:a))
# ╔═╡ 501ea091-049c-4bfc-86bf-d820dd59ecf7
length2(list(:a, :b))
# ╔═╡ 7ce00f81-566a-414d-9feb-b25907f5443d
length2(odds)
# ╔═╡ 589badfa-1f1c-4f1e-8ca2-06f98c34b53b
function append(list1, list2)
#--------------------------------------------------------------
null(list) = (car(list) == :nil) && (cdr(list) == :nil)
singleItem(list) = (car(list) !== :nil) && (cdr(list) == :nil)
#--------------------------------------------------------------
if null(list1)
list2
elseif singleItem(list1)
cons(car(list1), list2)
else
cons(car(list1), append(cdr(list1), list2))
end # if
end
# ╔═╡ b33110e1-8a30-4831-908e-833104b37494
function scale_list(list, factor)
#--------------------------------------------------------------
null(list) = (car(list) == :nil) && (cdr(list) == :nil)
singleItem(list) = (car(list) !== :nil) && (cdr(list) == :nil)
#--------------------------------------------------------------
if null(list)
list
elseif singleItem(list)
cons(car(list) * factor, :nil)
else
cons(car(list) * factor, scale_list(cdr(list), factor))
end
end
# ╔═╡ f62445fd-e6c0-4e3a-bc49-5f213a7f2192
function myMap(proc, list)
#--------------------------------------------------------------
null(list) = (car(list) == :nil) && (cdr(list) == :nil)
singleItem(list) = (car(list) !== :nil) && (cdr(list) == :nil)
#--------------------------------------------------------------
if null(list)
list
elseif singleItem(list)
cons(proc(car(list)), :nil)
else
cons(proc(car(list)), myMap(proc, (cdr(list))))
end # if
end
# ╔═╡ 74057de4-cf91-413d-b4f6-5e10ea2d5ece
myMap(abs, list(-10, 2.5, -11.6, 17)) # named nested tuple
# ╔═╡ 1a387789-4234-4a4b-80ae-1de62ca7162b
pp(myMap(abs, list(-10, 2.5, -11.6, 17))) # Scheme-like list
# ╔═╡ 06f3b046-2650-4755-aada-3865688fa612
myMap(x -> *(x, x), list(1, 2, 3, 4)) # named nested tuple
# ╔═╡ 7df7fc3c-5408-48ba-9a27-08bab8eebc8c
pp(myMap(x -> *(x, x), list(1, 2, 3, 4)))
# ╔═╡ b5054052-cb2d-4486-9b24-cdeb4eaf6aef
function scale_list2(list, factor)
myMap(x -> *(x, factor), list)
end
# ╔═╡ 4c522af5-eea5-458c-8c1c-b6403d4d9e69
scale_list2(list(), 10)
# ╔═╡ d01d3225-4bf0-4a94-9c08-155a137876f0
scale_list2(list(1, 2, 3, 4, 5), 10)
# ╔═╡ e07aa3d4-549e-4158-b1e7-8419872e8cc9
pp(scale_list2(list(1, 2, 3, 4, 5), 10))
# ╔═╡ 01d68de9-95f9-4c61-aa2d-8f4697dfcf8f
Tuple(cons(10, one_through_four2))
# ╔═╡ 621dbe13-519e-4ef1-8be8-258b3bbd9fc2
car(one_through_four2)
# ╔═╡ 7ae797c1-9dd8-499a-97fb-0d84001051e3
Tuple(cdr(one_through_four2))
# ╔═╡ 7f77a89d-e662-4857-aa4c-47d162fc09a0
car(cdr(one_through_four2))
# ╔═╡ e196666e-3a31-4f64-8ee6-caa9de9b0853
md"
###### 2nd (specialized) *typed* method of combined *selector* function $$cadr$$ ...
"
# ╔═╡ 273c3c3e-8f5f-4924-ac2f-1b3f7a17e5a0
cadr(list::Array) = car(cdr(list::Array))
# ╔═╡ 96e38418-c4e3-4563-aee8-fe75f8f7df9c
cadr(myList)
# ╔═╡ a9ffe340-608b-402b-b5bd-bc0b1e086528
cadr(one_through_four)
# ╔═╡ d5e77221-31e5-469b-99af-38953626bb77
cadr(one_through_four2)
# ╔═╡ bec12008-cf9e-4768-bccc-b39d83634617
md"
###### 3rd (default) *typed variant* method of *function* $$length$$ ...
###### ... implemented with $$while$$ and type $$NamedTuple{(:car, :cdr)}$$
"
# ╔═╡ a02680a3-5457-4e3d-ba31-54b257e2bd90
function length3(list::NamedTuple{(:car, :cdr)})::Signed
#-------------------------------------------------------------
null(list) = (car(list) == :nil) && (cdr(list) == :nil)
singleItem(list) = (car(list) !== :nil) && (cdr(list) == :nil)
#-------------------------------------------------------------
temp_list, count = list, 0
if null(temp_list)
count
else
while !singleItem(temp_list)
temp_list, count = cdr(temp_list), count + 1
end # while
count + 1
end # if
#-------------------------------------------------------------
end
# ╔═╡ cd34304a-8cc9-465d-bccc-18a9f5b9c121
length3(list())
# ╔═╡ 614c56ac-09a5-4f35-b92f-1d8cabbf6ecb
length3(list(:a))
# ╔═╡ 8728a302-43ab-468c-a051-6b0491674a88
length3(list(:a, :b))
# ╔═╡ ffe04f45-008e-4465-aeec-1c5c18f2157e
length3(odds)
# ╔═╡ 9d0e664d-8758-4b7a-bee7-6d077e70fe1e
# idiomatic Julia with broadcasting '.^2'
squares2 = list2(1,2,3,4,5).^2 # broadcasting over arrays does work
# ╔═╡ 812d7392-eca3-44fb-af0d-612b1534e961
Tuple(squares2)
# ╔═╡ bcc67c53-de8b-462e-b5d3-5825740e1b92
md"
###### 2nd (specialized) *typed method* of function $$list\_ref$$
"
# ╔═╡ 7177cb33-9202-4394-a1b2-89255bbdfcd4
function list_ref(list::Array, n)::Any
list[n+1]
end
# ╔═╡ 2844c5a8-b20d-4979-a3e5-533e6b83e7fc
list_ref(squares, 0) # for index == 0 it's the first element
# ╔═╡ 8c855187-3f21-47bf-93d8-4c1400674bf6
list_ref(squares, 0) == car(squares)
# ╔═╡ b022a613-510f-4f9b-9540-94718884b3f4
list_ref(squares, 3) # for index == 3 it's the 4th element
# ╔═╡ cf8956ba-61d3-4027-9d49-ad8cbfed97b4
list_ref(squares2, 3)
# ╔═╡ d1b4d1b1-7d6d-4059-b451-a97d798c4dd6
list_ref(squares2, 0)
# ╔═╡ 39d7227c-3586-4f62-875f-d72c08aa455a
list_ref(squares2, 3)
# ╔═╡ 8705f9e7-bffd-4ac0-a01b-7ce32f976c8c
odds2 = list2(1, 3, 5, 7)
# ╔═╡ cd5dd465-2084-483c-bcea-f508c6aead84
Tuple(odds2)
# ╔═╡ 8ee31694-4e27-49a2-9570-a586cd998d91
md"
###### 2nd (specialized) *typed* method of function $$length$$ ...
###### ... implemented with Julia's $$Array$$
"
# ╔═╡ 8b8671fd-2af1-4b59-beca-1ce8ac51dbc7
function length(list::Array)::Int
lastindex(list)
end
# ╔═╡ 54fa7582-ca53-495c-bcfc-73b57c01d9a8
length(odds)
# ╔═╡ 97d8ea55-d515-4eb2-a907-17cdc9be51d4
length(odds2)
# ╔═╡ b12de1c2-bfef-4679-975b-141ad169e11a
md"
###### 2nd (specialized) *typed* method of function $$append$$ ...
###### ... implemented with Julia's $$Array$$
"
# ╔═╡ 9ddf31fb-8fed-42f9-9e0d-c0c75f008cee
function append(list1::Array, list2::Array)::Array
#------------------------------------
null(list::Array)::Bool = list == []
#------------------------------------
if null(list1)
list2
else
cons(car(list1), append(cdr(list1), list2))
end
end
# ╔═╡ 6048f7e3-5570-4449-9c60-de2f44794b2a
append(squares, odds)
# ╔═╡ 0e0d818b-d998-4a8b-b4bf-f8a88ab7dc4c
pp(append(squares, odds))
# ╔═╡ 00414b7f-b1d9-424e-ab38-2a1a1d64da8f
append(odds, squares)
# ╔═╡ eb27d136-d979-43d9-b102-600f82870792
pp(append(odds, squares))
# ╔═╡ 8741dc08-700c-4d7e-a94d-2cfdbe48cf1e
Tuple(append(squares2, odds2))
# ╔═╡ d601bdb9-f736-450c-b1e9-fe291f13b2b6
Tuple(append(odds2, squares2))
# ╔═╡ 8060aeae-6cbc-4b26-b9f1-29d572b7bab3
first(odds2)
# ╔═╡ 87e8768c-d562-444d-be5a-176fa08a393d
last(odds2)
# ╔═╡ 811ca574-a55c-44e0-890f-388877804f4e
Tuple(copy(odds2[1:3]))
# ╔═╡ 3108621b-98a7-4c97-9a33-445b766e04f5
function append2(list1::Array, list2::Array)::Array
#------------------------------------
null(list::Array)::Bool = list == []
#------------------------------------
function append_iter(list1::Array, list1_list2::Array)
if null(list1)
list1_list2
else
append_iter(list1[1:end-1], cons(last(list1),list1_list2))
end
end
append_iter(list1, list2)
end
# ╔═╡ 8d40ce9b-55ec-48fb-a6c1-74cbf640a44d
Tuple(append2(squares2, odds2))
# ╔═╡ c5ea6612-8793-49c8-b8db-f033f9bedfff
Tuple(append2(odds2, squares2))
# ╔═╡ 96fdc3f7-e75a-4697-b48a-ee7dac5301c8
function append3(list1::Array, list2::Array)::Array
#------------------------------------
null(list::Array)::Bool = list == []
#------------------------------------
list1_list2 = list2
while !null(list1)
list1, list1_list2 = list1[1:end-1], cons(last(list1),list1_list2)
end
list1_list2
end
# ╔═╡ 56863dba-89b3-4e14-b627-d79874cc477b
Tuple(append3(squares2, odds2))
# ╔═╡ df552faa-0cb4-427c-a1e2-3c274e107c68
Tuple(append3(odds2, squares2))
# ╔═╡ 53388b16-6443-4aac-b6b6-d23a8e8b7c9d
function append4(list1::Array, list2::Array)::Array
vcat(list1, list2)
end
# ╔═╡ bff34be8-d69f-4c32-8355-bff70babd128
Tuple(append4(squares2, odds2))
# ╔═╡ 2e4b1c5c-a6b1-447b-986f-6209cd74a9c3
Tuple(append4(odds2, squares2))
# ╔═╡ 5f38b665-766a-4032-a26b-036b476790a2
md"
##### Mapping over lists
"
# ╔═╡ af1109e0-47ad-4ab2-8209-27bb8f880bb5
md"
###### 2nd (specialized) *typed* method of function $$scale\_list$$ ...
###### ... implemented with Julia's *broadcasting* over $$Array$$s
"
# ╔═╡ 50b38ed0-84c2-4dac-b2ec-9f21e6ae1333
function scale_list(list::Array, factor::Number)::Array
list.*factor
end
# ╔═╡ 0b7050d5-1824-407a-b14e-42e2b96c367b
scale_list(list(), 10)
# ╔═╡ 689180cf-ab0d-4488-a272-ca960a37c3f4
pp(scale_list(list(), 10)) # pretty print
# ╔═╡ ac560a9b-faac-47d1-948c-88d94fee7fcc
scale_list(list(1, 2, 3, 4, 5), 10) # named nested tuple
# ╔═╡ 6daa1278-575f-4b8e-bd2f-0db8e6ccf7f3
pp(scale_list(list(1, 2, 3, 4, 5), 10))
# ╔═╡ 878d99ae-14e3-46e5-b31a-74af7df6e039
Tuple(scale_list(list2(1, 2, 3, 4, 5), 10))
# ╔═╡ f5af7525-2be7-48a5-b938-c2344b6f2bff
typeof(scale_list(list2(1, 2, 3, 4, 5), 10))
# ╔═╡ 7ef4344c-2049-49eb-882b-10239ce9ec3b
Tuple(list2(-10, 2.5, -11.6, 17))
# ╔═╡ 2a76cc50-c918-45b8-ab64-8d03228f113e
Tuple(map(abs, list2(-10, 2.5, -11.6, 17)))
# ╔═╡ 7a1d9398-453b-4616-97c2-c6e463f78b25
map(list2(-10, 2.5, -11.6, 17)) do item
abs(item)
end
# ╔═╡ 89e4b01a-1ad7-486a-994c-f53061de9f04
map(x -> x^2, list2(1, 2, 3, 4))
# ╔═╡ 17aaf21c-d037-45a9-bbbf-63218db57adf
map(list2(1, 2, 3, 4)) do item
item^2
end
# ╔═╡ 454a1c03-7353-4909-b244-13ffd61fd9ea
function scale_list3(list::Array, factor::Number)::Array
map(list) do item
item*factor
end # map
end
# ╔═╡ 17180638-2950-4f62-801a-b6eeedda28ef
scale_list3(list2(1, 2, 3, 4, 5), 10)
# ╔═╡ a43f0bce-8507-4b33-9862-f11999071a59
md"
##### References
- **Abelson, H., Sussman, G.J. & Sussman, J.**; Structure and Interpretation of Computer Programs, Cambridge, Mass.: MIT Press, (2/e), 1996, [https://sarabander.github.io/sicp/](https://sarabander.github.io/sicp/), last visit 2022/09/04
"
# ╔═╡ 41e8c05a-fd5e-4d94-a14c-ed60b6570d59
md"
---
##### end of ch. 2.2.1
====================================================================================
This is a **draft** under the [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](https://creativecommons.org/licenses/by-nc-sa/4.0/) license. Comments, suggestions for improvement and bug reports are welcome: **claus.moebus(@)uol.de**
====================================================================================
"
# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
"""
# ╔═╡ 00000000-0000-0000-0000-000000000002
PLUTO_MANIFEST_TOML_CONTENTS = """
# This file is machine-generated - editing it directly is not advised
julia_version = "1.8.5"
manifest_format = "2.0"
project_hash = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
[deps]
"""
# ╔═╡ Cell order:
# ╟─373a14a0-036b-11ec-3063-73539b537973
# ╟─278e3d83-0583-459a-adec-95843b3314fd
# ╟─15d15f7e-ae79-4f36-87ca-0363ea9f598e
# ╟─b2eb614e-8369-40f1-a7df-2551a3e20c9d
# ╠═ecbb360b-42d8-4ee8-8356-2028b1e5ccba
# ╟─76b9bc7c-8afd-4e1d-a727-7646f377b09d
# ╠═f7000508-5beb-4d1f-8d9f-ca0dd98d94af
# ╟─100ac0a5-9f7d-4563-9fac-03967cb82e17
# ╠═e4bc1b01-7e3f-41ad-b054-e52912c9357c
# ╟─5a6d3aa0-cd51-4c8a-a5cd-4cb1082b5135
# ╠═9abffca7-23c0-4287-8b54-f69d1f95a19f
# ╠═78829665-5360-4c7a-ae09-b664560e4749
# ╠═d404efa1-dc5f-40bb-8f10-bfb93dbd09ed
# ╟─b4ef7e20-499d-4019-a694-f5e88c3e0d67
# ╠═e980b7d8-1c8b-4354-9bbd-313539b5c6ec
# ╟─49d36791-24d6-4c7e-bec6-7593e8889478
# ╠═8203d83e-9ba8-40c2-8fb1-1f0f1f4a508c
# ╟─4f343982-2aba-40ff-9d9d-f630ed4673f7
# ╠═77a6bc9b-6948-40f7-85f4-862e1824d436
# ╟─04a3c30a-ccce-469c-9fa5-2b451082be8a
# ╠═6f28288b-0cd9-4d32-b80b-1145a582a562
# ╠═b5d38b3c-8033-4898-af30-e385cb24cdd8
# ╠═3c1528ac-63ed-4944-9afb-6e10b9b26793
# ╠═1627b84f-d538-4d11-bd1e-2e2f3082e779
# ╟─b491dcdb-e51b-4438-8a26-e975e2a50533
# ╠═25ae5618-cbc6-4325-bfb9-7be558102b9e
# ╠═04de1c1f-7c2c-4a07-89ad-f7fadc11d04a
# ╠═cb1bd172-6e08-42d2-beb1-7278e5961f83
# ╠═165c5ded-6f9f-42cd-9ece-d54a40e847e3
# ╠═df60c1a7-3f91-4370-8232-135bfff959d1
# ╠═5cec6d4d-4124-46a4-9400-d3c7bfeaa2b2
# ╟─47ff8a5c-4682-48c7-8080-272a73f38c3d
# ╟─54b14609-6552-40e9-9410-cbf8b8694a04
# ╟─531f20b7-7205-4c84-a0eb-63ddddc41180