-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathch02.jl
360 lines (270 loc) · 4.57 KB
/
ch02.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
# Bogumił Kamiński, 2021
# Codes for chapter 2
# Code for listing 2.1
1
true
"Hello world!"
0.1
[1, 2, 3]
# Code for listing 2.2
typeof(1)
typeof(true)
typeof("Hello world!")
typeof(0.1)
typeof([1, 2, 3])
# Code for showing bit representation of numbers
bitstring(1)
bitstring(1.0)
bitstring(Int8(1))
# Code showing to what Int alias expands
Int
# Code for checking if value is of some type
[1, 2, 3] isa Vector{Int}
[1, 2, 3] isa Array{Int64, 1}
# Code for section 2.2
x = 1
y = [1, 2, 3]
x = 1
x
typeof(x)
x = 0.1
x
typeof(x)
Kamiński = 1
x₁ = 0.5
ε = 0.0001
# - change to help mode by pressing `?` key
# - type (or copy-paste) "₁" and press Enter
# - change to help mode by pressing `?` key
# - type (or copy-paste) "ε" and press Enter
# Code for listing 2.3
x = -7
if x > 0
println("positive")
elseif x < 0
println("negative")
elseif x == 0
println("zero")
else
println("unexpected condition")
end
# Code showing that logical condition must be Bool
x = -7
if x
println("condition was true")
end
# Code showing comparisons against NaN
NaN > 0
NaN >= 0
NaN < 0
NaN <= 0
NaN == 0
NaN != 0
NaN != NaN
# Code showing that floating point arithmetic is only approximate
0.1 + 0.2 == 0.3
0.1 + 0.2
isapprox(0.1 + 0.2, 0.3)
0.1 + 0.2 ≈ 0.3
# Code showing combining conditions
x = -7
x > 0 && x < 10
x < 0 || log(x) > 10
x = -7
log(x)
# Code showing typical one-line conditional execution expressions
x > 0 && println(x)
if x > 0
println(x)
end
x > 0 || println(x)
if !(x > 0)
println(x)
end
x = -7
x < 0 && println(x^2)
iseven(x) || println("x is odd")
x = -7
if x < 0
println(x^2)
end
if !iseven(x)
println("x is odd")
end
x = -7
if x < 0 && x^2
println("inside if")
end
# Code showing ternary operator
x > 0 ? sqrt(x) : sqrt(-x)
if x > 0
sqrt(x)
else
sqrt(-x)
end
x = -7
x > 0 ? println("x is positive") : println("x is not positive")
# Code showing that conditional statements return a value
x = -4.0
y = if x > 0
sqrt(x)
else
sqrt(-x)
end
y
x = 9.0
y = x > 0 ? sqrt(x) : sqrt(-x)
y
# Code for listing 2.4
for i in [1, 2, 3]
println(i, " is ", isodd(i) ? "odd" : "even")
end
# Code for listing 2.5
i = 1
while i < 4
println(i, " is ", isodd(i) ? "odd" : "even")
global i += 1
end
# Code showing break and continue keywords
i = 0
while true
global i += 1
i > 6 && break
isodd(i) && continue
println(i, " is even")
end
# Code for listing 2.6
x = -7
x < 0 && begin
println(x)
x += 1
println(x)
2 * x
end
x > 0 ? (println(x); x) : (x += 1; println(x); x)
# Code for section 2.3.4
x = [8, 3, 1, 5, 7]
k = 1
y = sort(x)
for i in 1:k
y[i] = y[k + 1]
y[end - i + 1] = y[end - k]
end
y
s = 0
for v in y
global s += v
end
s
s / length(y)
function f!(x)
x[1] = 10
return x
end
x = [1, 2, 3]
f!(x)
x
# Code for listing 2.7
function times_two(x)
return 2 * x
end
times_two(10)
# Code for listing 2.8
function compose(x, y=10; a, b=10)
return x, y, a, b
end
compose(1, 2; a=3, b=4)
compose(1, 2; a=3)
compose(1; a=3)
compose(1)
compose(; a=3)
# Code for listing 2.9
times_two(x) = 2 * x
compose(x, y=10; a, b=10) = x, y, a, b
# Code showing the use of map function
map(times_two, [1, 2, 3])
# Code for listing 2.10
map(x -> 2 * x, [1, 2, 3])
# Code showing sum taking a function as a first argument
sum(x -> x ^ 2, [1, 2, 3])
# Code showing do-end syntax
sum([1, 2, 3]) do x
println("processing ", x)
return x ^ 2
end
# Code showing the difference between sort and sort!
x = [5, 1, 3, 2]
sort(x)
x
sort!(x)
x
# Code showing a simple implementation of winsorized_mean function
function winsorized_mean(x, k)
y = sort(x)
for i in 1:k
y[i] = y[k + 1]
y[end - i + 1] = y[end - k]
end
s = 0
for v in y
s += v
end
return s / length(y)
end
winsorized_mean([8, 3, 1, 5, 7], 1)
# Code for section 2.5
function fun1()
x = 1
return x + 1
end
fun1()
x
function fun2()
if true
x = 10
end
return x
end
fun2()
function fun3()
x = 0
for i in [1, 2, 3]
if i == 2
x = 2
end
end
return x
end
fun3()
function fun4()
for i in [1, 2, 3]
if i == 2
x = 2
end
end
return x
end
fun4()
function fun5()
for i in [1, 2, 3]
if i == 1
x = 1
else
x += 1
end
println(x)
end
end
fun5()
function fun6()
x = 0
for i in [1, 2, 3]
if i == 1
x = 1
else
x += 1
end
println(x)
end
end
fun6()