-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCM20210831_SICP_2.3.1_Quotation.jl
309 lines (250 loc) · 8.58 KB
/
PCM20210831_SICP_2.3.1_Quotation.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
### A Pluto.jl notebook ###
# v0.19.11
using Markdown
using InteractiveUtils
# ╔═╡ 353d3930-0a7c-11ec-22fb-2ba2ded89523
md"
======================================================================================
#### [SICP\_2.3.1\_Quotation.jl](https://sarabander.github.io/sicp/html/2_002e3.xhtml#g_t2_002e3_002e1)
###### file: PCM20210831\_SICP\_2.3.1\_Quotation.jl
###### Julia/Pluto.jl-code (1.8.1/19.11) by PCM *** 2022/09/12 ***
======================================================================================
"
# ╔═╡ 54d272ec-9bb2-4d71-a712-9bd3ca735359
struct Cons
car
cdr
end # struct Cons
# ╔═╡ 98d8a477-ef35-4413-b5bf-16ca522b5211
md"
###### 3 methods of function $$cons$$
"
# ╔═╡ 4fd917ac-bff2-4ade-af7d-68feab1d5de8
cons(car::Any, cdr::Any)::Cons = Cons(car, cdr)::Cons
# ╔═╡ 4f9cf503-e6c0-4fcf-bfca-5219e086423e
function cons(car::Any, list2::Vector)::Vector
conslist = list2
pushfirst!(conslist, car)
conslist
end # function cons
# ╔═╡ 1f6f23aa-571b-4b0a-b700-4faf6da3e781
function cons(list1::Vector, list2::Vector)::Vector
conslist = push!([], list1)
for xi in list2
push!(conslist, xi)
end # for
conslist
end # function cons
# ╔═╡ 41911af6-dc2d-4088-ab2c-c239f92b2136
md"
###### 2 methods of function $$car$$
"
# ╔═╡ 1f75b84e-babe-4f6c-af9c-ca7247d8c626
car(cell::Cons) = cell.car
# ╔═╡ c3e0b7ba-115d-499c-9f30-21d3dc3a92cd
car(x::Vector) = x[1]
# ╔═╡ 05ca7292-f9a0-4c30-bec5-094062be6199
md"
###### 2 methods of function $$cdr$$
"
# ╔═╡ 3b7b8373-2a1d-400c-a67c-b6ad69d5a4f1
cdr(cell::Cons)::Any = cell.cdr
# ╔═╡ c0f13087-edc7-433b-b715-e408386e11f2
cdr(x::Vector) = x[2:end]
# ╔═╡ 35d094a8-a129-4d0b-b4eb-bd714f6e71c6
md"
###### Definition of symbols
"
# ╔═╡ f4dd7244-0618-457d-90c6-3f5479d1f35b
const a = 1
# ╔═╡ 6c5718e3-aaae-47f9-a98d-33e7c9f56d30
const b = 2
# ╔═╡ d3c5964f-b96f-482d-a4a8-f587d3244192
md"
###### Definition of Scheme-like function $$list$$
"
# ╔═╡ 4361cbda-74d6-4716-942d-b4a37d545ed5
function list(xs...)
push!([], xs...)
end
# ╔═╡ 67a71799-3e40-48ef-bd29-a8bba877ed7a
list(:a, :b, :c, :d)
# ╔═╡ 04caf83a-9df8-41e2-835d-1bbfc7e6698d
list(23, 45, 17)
# ╔═╡ 0a507b0a-b377-4215-b5ff-cd377162357d
list(list(:Norah, 12), list(:Molly, 9), list(:Anna, 7), list(:Lauren, 6), list(:Charlotte, 4))
# ╔═╡ 6741b890-392a-48f3-b970-b2a289862875
list(a, b)
# ╔═╡ 4ce122e0-9a41-4b66-bf06-501f42aaea46
list(:a, :b)
# ╔═╡ a360ee12-21e7-4742-9469-d8724be60546
list(:a, b)
# ╔═╡ 2b1ee845-cca0-42fb-a88e-987d26445f8e
:(a) == :a
# ╔═╡ 80d84f79-9654-470b-b21f-0b0fb5be1f80
:(a, b, c)
# ╔═╡ 65251062-5e93-4575-b5e0-5b4e489ec870
typeof(:(a, b, c))
# ╔═╡ d09851a1-76e8-48c6-b928-1541996e77c8
dump(:(a, b, c))
# ╔═╡ 63ba970c-88f8-4586-b042-27527d2d8e9f
:(a, b, c).head
# ╔═╡ b0b672ef-e8d5-4712-bb33-015279d651ae
:(a, b, c).args
# ╔═╡ b1bf6c2b-154b-46dc-80c3-2c931e00c52f
:(a, b, c).args[1]
# ╔═╡ ff0f7f24-23d9-4481-8990-b098c31bd780
:(a, b, c).args[2:end]
# ╔═╡ 2ae5f8a3-5b8c-4551-8220-870d40e0eb5d
function memq1(item, list)
#---------------------------------------
null = isempty
#---------------------------------------
if null(list)
false
elseif item == car(list)
list
else memq1(item, cdr(list))
end # if
end # function memq1
# ╔═╡ 8dbe5da1-6a52-4beb-8be3-2a49cd248edb
memq1(:apple, [:pear, :banana, :prune])
# ╔═╡ c591ac8a-b2d8-4d11-b416-1971077cf416
memq1(:apple, [:x, [:apple, :sauce], :y, :apple, :pear, :prune])
# ╔═╡ f6b18ff5-05b6-413a-8cde-a4f364fca029
md"
###### idiomatic Julia with $$while, return$$
"
# ╔═╡ f46b37a5-6112-4931-bc77-deb58a1f1dab
# idiomatic Julia with 'while' and 'return'
function memq2(item, list)
#---------------------------------------
null = isempty
#---------------------------------------
while !null(list)
if item == car(list)
return list
else
list = cdr(list)
end # if
end # while
false
end # function memq2
# ╔═╡ b7055361-cda1-4eb9-b32e-49675a851385
memq2(:apple, [:pear, :banana, :prune])
# ╔═╡ faf28e99-40de-464b-8a3a-011689687d30
memq2(:apple, [:x, [:apple, :sauce], :y, :apple, :pear, :prune])
# ╔═╡ d3f74e3d-a30f-49d0-a68d-0bcbd3342be7
# idiomatic Julia with 'while' and 'return'
function memq3(item, list)
#---------------------------------------
null = isempty
#---------------------------------------
for i in eachindex(list)
if item == car(list)
return list
else
list = cdr(list)
end # if
end # for
false
end # function memq3
# ╔═╡ 8a179b6d-a76f-4bfd-b47c-1d4af7797f92
memq3(:apple, [:pear, :banana, :prune])
# ╔═╡ f9f772a3-805f-4b99-ace4-9e17c26e5302
memq3(:apple, [:x, [:apple, :sauce], :y, :apple, :pear, :prune])
# ╔═╡ 67816956-b1c3-4334-976f-26b3a1852a81
md"
###### idiomatic Julia with $$findfirst$$
"
# ╔═╡ fb134d51-0c6c-4258-a4ef-903d62d94ef5
findfirst((x -> x == :apple), [:x, [:apple, :sauce], :y, :apple, :pear, :prune])
# ╔═╡ 6c079e06-311b-4bb2-a8e2-3c7b154a4ba8
function memq4(item, list)
index = findfirst(x -> x == item, list)
if index !== nothing
list[index:end]
else
index
end # if
end # function memq4
# ╔═╡ db3624af-6052-453c-8e76-140d14d091eb
memq4(:apple, [:pear, :banana, :prune])
# ╔═╡ fb395bf2-da59-482c-afad-83668c8b674a
memq4(:apple, [:x, [:apple, :sauce], :y, :apple, :pear, :prune])
# ╔═╡ cc75410b-3f61-47e1-9d86-311a14b6681e
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/11
---
"
# ╔═╡ 02ace1e9-4a37-48a8-871f-8136837241a6
md"
###### end of ch. 2.3.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.1"
manifest_format = "2.0"
project_hash = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
[deps]
"""
# ╔═╡ Cell order:
# ╟─353d3930-0a7c-11ec-22fb-2ba2ded89523
# ╠═54d272ec-9bb2-4d71-a712-9bd3ca735359
# ╟─98d8a477-ef35-4413-b5bf-16ca522b5211
# ╠═4fd917ac-bff2-4ade-af7d-68feab1d5de8
# ╠═4f9cf503-e6c0-4fcf-bfca-5219e086423e
# ╠═1f6f23aa-571b-4b0a-b700-4faf6da3e781
# ╟─41911af6-dc2d-4088-ab2c-c239f92b2136
# ╠═1f75b84e-babe-4f6c-af9c-ca7247d8c626
# ╠═c3e0b7ba-115d-499c-9f30-21d3dc3a92cd
# ╟─05ca7292-f9a0-4c30-bec5-094062be6199
# ╠═3b7b8373-2a1d-400c-a67c-b6ad69d5a4f1
# ╠═c0f13087-edc7-433b-b715-e408386e11f2
# ╟─35d094a8-a129-4d0b-b4eb-bd714f6e71c6
# ╠═67a71799-3e40-48ef-bd29-a8bba877ed7a
# ╠═04caf83a-9df8-41e2-835d-1bbfc7e6698d
# ╠═0a507b0a-b377-4215-b5ff-cd377162357d
# ╠═f4dd7244-0618-457d-90c6-3f5479d1f35b
# ╠═6c5718e3-aaae-47f9-a98d-33e7c9f56d30
# ╟─d3c5964f-b96f-482d-a4a8-f587d3244192
# ╠═4361cbda-74d6-4716-942d-b4a37d545ed5
# ╠═6741b890-392a-48f3-b970-b2a289862875
# ╠═4ce122e0-9a41-4b66-bf06-501f42aaea46
# ╠═a360ee12-21e7-4742-9469-d8724be60546
# ╠═2b1ee845-cca0-42fb-a88e-987d26445f8e
# ╠═80d84f79-9654-470b-b21f-0b0fb5be1f80
# ╠═65251062-5e93-4575-b5e0-5b4e489ec870
# ╠═d09851a1-76e8-48c6-b928-1541996e77c8
# ╠═63ba970c-88f8-4586-b042-27527d2d8e9f
# ╠═b0b672ef-e8d5-4712-bb33-015279d651ae
# ╠═b1bf6c2b-154b-46dc-80c3-2c931e00c52f
# ╠═ff0f7f24-23d9-4481-8990-b098c31bd780
# ╠═2ae5f8a3-5b8c-4551-8220-870d40e0eb5d
# ╠═8dbe5da1-6a52-4beb-8be3-2a49cd248edb
# ╠═c591ac8a-b2d8-4d11-b416-1971077cf416
# ╟─f6b18ff5-05b6-413a-8cde-a4f364fca029
# ╠═f46b37a5-6112-4931-bc77-deb58a1f1dab
# ╠═b7055361-cda1-4eb9-b32e-49675a851385
# ╠═faf28e99-40de-464b-8a3a-011689687d30
# ╠═d3f74e3d-a30f-49d0-a68d-0bcbd3342be7
# ╠═8a179b6d-a76f-4bfd-b47c-1d4af7797f92
# ╠═f9f772a3-805f-4b99-ace4-9e17c26e5302
# ╟─67816956-b1c3-4334-976f-26b3a1852a81
# ╠═fb134d51-0c6c-4258-a4ef-903d62d94ef5
# ╠═6c079e06-311b-4bb2-a8e2-3c7b154a4ba8
# ╠═db3624af-6052-453c-8e76-140d14d091eb
# ╠═fb395bf2-da59-482c-afad-83668c8b674a
# ╟─cc75410b-3f61-47e1-9d86-311a14b6681e
# ╟─02ace1e9-4a37-48a8-871f-8136837241a6
# ╟─00000000-0000-0000-0000-000000000001
# ╟─00000000-0000-0000-0000-000000000002