-
Notifications
You must be signed in to change notification settings - Fork 2
/
chapter1.txt
576 lines (363 loc) · 12.5 KB
/
chapter1.txt
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
#title Chapter 1: atoms, tuples and lists
%% [WS] Should start even more basic. Explain statements. Why is there a '.' at the end of "every line of code" here.
Is it true that this is an atom?
dog.
Yes,
because dog is a string of characters beginning with the letter d.
----
Is it true that this is an atom?
john.
Yes,
because john is a string of characters beginning with a letter.
----
Is it true that this is an atom?
h.
Yes,
because h is a string of one character, which is a letter.
----
Is it true that this is an atom?
'*abc$'.
Yes,
because surrounding any character string with single quotes makes it an atom.
----
Is it true that Foo is an atom?
False,
A string of characters starting with an upper case letter is
a variable.
----
Is it true that Foo is bound to the atom john in
Foo = john.
True,
The variable Foo is bound to the atom john by using the = operator.
----
If Foo is bound to john, is the following statement true?
Foo = chicken.
False,
variables can only be assigned once. This is called single assignment. Once Foo is bound to the atom john it cannot be rebound to another value*.
[1] In some cases I will reuse a variable in separate examples which would require rebinding it. If you are following along at the REPL and you have a variable called Head you can make the variable unbound by using the command f(Head). This should only be used for testing purposes at the REPL and should never be used in an attempt to override single assignment variables.
----
Is it true that this is a tuple?
{john, likes, erlang}.
True,
a tuple is a way to store a known number of terms by enclosing them in curly braces.
----
Is it true that this is a tuple?
{john}.
True,
it is an atom enclosed by curly braces which makes it a tuple.
----
Is it true that this is a tuple?
{john, {really, likes}, erlang}.
True,
a tuple can hold any Erlang term.
----
True or false: We can store tuples in variables
Foo = {this, is, a, tuple}.
True,
variables can hold any Erlang term, which includes tuples.
----
True or false: We can build tuples that hold variables
Foo = {this, is, a, tuple}.
Bar = {another, tuple, Foo}.
True,
since a tuple can hold any Erlang term, we can put variables into tuples as well. This example would expand to. {another, tuple, {this, is, a, tuple}}.
----
What is the value of Foo where
{john, likes, Foo} = {john, likes, erlang}.
erlang,
we can extract all or some of the values from a tuple using pattern matching. In this case the atoms john and likes are the same on both sides, so they match. We also have the unbound variable Foo on the left hand side which matches to the atom erlang on the right hand side. This will assign the atom erlang to the variable Foo.
----
What is the value of Foo where
{another, tuple, Foo} = {another, tuple, {this, is, a, tuple}}.
{this, is, a, tuple},
since the last term in the tuple on the right is another tuple, it will become bound to Foo if Foo is an unbound variable.
----
If Foo is bound to the tuple {this, is, a, tuple} what is the outcome of the following statement?
{Foo, tuple, {this, is, a, tuple}} = {another, tuple, {this, is, a, tuple}}.
Error!,
you will receive an error that looks something like this
** exception error: no match of right hand side value {another,tuple,
{this,is,a,tuple}}
because the expression is trying to match the bound variable Foo, which is assigned the tuple {this, is, a, tuple}, with the atom another. If you tried to do this by itself it would look like
{this, is, a, tuple} = another.
which is obviously not a match.
----
True or false: We can ignore some elements of a tuple while extracting others by using
{_, _, Foo} = {another, super, tuple}.
True,
by using the anonymous variable _ we can let the interpreter know that we don’t care about the first or second values of the tuple and that we only want the third term. We will discuss anonymous variables a little later on.
----
Is it true that this is a list?
[atom].
Yes,
because [atom] is an atom enclosed by square brackets
----
Is it true that this is a list?
[atom, turkey, org].
Yes,
because it is a collection of atoms, separated by commas and enclosed by square brackets.
----
Is it true that this is a list?
[atom, turkey], org.
No,
because these are two separate terms not enclosed by square brackets. The first one is a list containing
two atoms, and the second one is an atom.
----
Is it true that this is a list?
[[atom, turkey], org].
Yes,
because the two terms are now enclosed by square brackets and separated by a comma.
----
Is it true that this is a term?
xyz.
Yes,
because all atoms are terms.
----
Is it true that this is a term?
[x,y,z].
Yes,
because it is a list.
----
Is it true that this is a term?
[[x,y],z].
Yes,
because all lists are terms.
----
Is it true that this is a list?
[erlang, is, great].
Yes,
because it is a collection of terms enclosed by square brackets.
----
How many terms are in the list
[erlang, is, great].
Three,
each of the atoms in the list are a term
----
Is it true that this is a list?
[[[how], are], [[you], [doing, so]], far].
Yes,
because it is a collection of terms enclosed by square brackets.
----
How many terms are in the list
[[[how], are], [[you], [doing, so]], far].
and what are they?
Three,
[[how], are]
[[you], [doing, so]]
and far
----
Is it true that this is a list?
[].
Yes,
square brackets by themselves create an empty list.
----
Is it true that this is an atom?
[].
No,
because [] is just a list.
----
Is it true that this is a list?
[[],[],[],[]].
Yes,
because it is a collection of empty lists.
----
What is the value of the variable Head where
[Head|_] = [a,b,c].
a,
because a is the first atom of this list.
----
What is the value of Head where
[Head|_] = [[a,b,c], x,y,z].
[a,b,c],
because [a,b,c] is the first term of this non-empty list.
----
What is the value of Head where
[Head|_] = hotdog.
No answer,
you cannot ask for the head of an atom.
----
What is the value of Head where
[Head|_] = [].
No answer,
You cannot ask for the head of an empty list.
----
What is the value of Head where
[Head|_] = [[ [hotdogs] ], ['and'], [pickle], relish].
[ [hotdogs] ],
read as: "The list of the list of hotdogs" [ [hotdogs] ] is the first term of the list.
----
What is the value of Head where
[ [ Head | _ ] | _ ] = [[ [hotdogs] ], ['and']].
[hotdogs]
----
What is the value of Tail where
[ _ | Tail] = [a,b,c].
[b,c],
because [b,c] is the list without [Head|_].
----
What is the value of Tail where
[ _ |Tail] = [[a,b,c], x,y,z].
[x,y,z]
----
What is the value of Tail where
[ _ |Tail] = hotdogs.
No answer,
You cannot ask for the Tail of an atom.
----
What is the value of Tail where
[ _ |Tail] = [].
No answer,
You cannot ask for the Tail of an empty list.
----
What is the value of Second where
[_, Second | Tail] = [[b], [x,y], [ [c] ]].
[x,y],
because we can access the second term in the list by using an anonymous variable for the head term, then include
a comma, followed by the variable Second which will match the second element in the list. If you know how long a
list is, you can access any element this way by separating each term with a comma until you have all of the terms
off of the head that you are needing.
----
What is the value of Tail where
[_ , _ | Tail] = [[b], [x,y], [ [c] ]].
[[ [c] ]],
because we are able to take the first two terms off of the head of the list leaving only the last element remaining
in the list.
----
What is the value of Tail where
[[_ |Tail]|_ ] = [a, [b, [c]], d].
No answer,
since [_ |Tail] is the Head of the list and the Head of [a, [b, [c]], d]
is an atom, and [_ |Tail] does not take an atom as an argument.
----
What does [Head|_] match against?
It will match against any non-empty list.
----
What does [_ |Tail] match against?
It will match against any non-empty list.
----
What does [_, Second | _] match against?
It will match against the second term in any list containing at least two terms.
---
What is the value of _ where
[_ |Tail] = [a,b,c].
Nothing,
_ is an anonymous variable used in pattern matching when a variable is
required but it’s value can be ignored. It has been used in our examples of
[Head|_] and [_ |Tail]. In these cases we have not cared about the value
where _ is used so we chose to use an anonymous variable and ignore the
value.
----
What is the resulting list when you add the atom peanut to the beginning of the list [butter, bacon, jelly]
This can be written as
[peanut | [butter, bacon, jelly]].
[peanut, butter, bacon, jelly],
because adding peanut to the [Head|_] position will add it onto the existing list.
----
What is the resulting list when you add the atom jelly to the end of the list
[peanut, butter, bacon]
This can be written as
[[peanut, butter, bacon] | jelly].
[[peanut, butter, bacon] | jelly],
because adding jelly to the [_ |Tail] of the list will only create a new list with two terms, the old list and the atom jelly, it will not create a unified list as it does
when you add something on to the [Head|_] of the list.
----
How can we create a flat list using an atom and a list?
[term | list],
because adding an atom to the [Head|_] of a list will create a new flat list. You can add any valid term to the head of the list, it is not restricted to atoms.
----
How can we create a new list that contains a list and an atom?
[list | atom],
because adding an atom to the [_|Tail] of a list will create a new list containing
a list and an atom.
----
Is is_atom(S) true or false where
S = harry.
True,
because is_atom(S) is just another way to ask “Is S an atom?”
----
Is is_atom(S) true or false where
S = of.
Syntax Error,
because of is one of the 28 reserved words in Erlang. The full list of reserved words is:
after and andalso band begin bnot bor bsl bsr bxor case catch cond div end fun if let not of or orelse query receive rem try when xor
----
Is is_atom(S) true or false where
S = 'of'
True,
a string of characters enclosed by single quotes is an atom and will not cause a syntax error when used with reserved words.
----
Is is_atom(S) true or false where
S = [harry, had, a, heap, ‘of’, apples].
False,
since S is a list.
----
How many arguments does is_atom() take and what are they?
It takes one argument. The argument can be any term.
----
Is is_atom(Head) true or false where
[Head|_] = [harry, had, a, heap, ‘of’, apples].
True,
because the Head of the list would be the atom harry.
----
Is is_atom(Tail) true or false where
[_ |Tail] = [harry, had, a, heap, ‘of’, apples].
False,
because the Tail of the list would be a list.
----
Is is_atom(Tail) true or false where
[_ |Tail] = [harry].
False,
because the list [] is not an atom.
----
Is is_atom(Second) true or false where
[_, Second | _] = [swing, low, sweet, cherry, oat].
True,
because Second would match the atom low.
----
Is is_atom(Second) true or false where
[_, Second | _ ] = [swing, [low, sweet], cherry, oat].
False,
the second term in the list is the list [low, sweet].
----
Is is_list(S) true or false where
S = harry.
False,
because is_list(S) is just another way to ask “Is S a list?”
----
Is is_list(S) true or false where
S = [harry, had, a, heap, ‘of’, apples].
True,
since S is a list.
----
How many arguments does is_list() take and what are they?
It takes one argument. The argument can be any term.
----
Is is_list(Head) true or false where
[Head|_] = [harry, had, a, heap, ‘of’, apples].
False,
because the Head of the list would be the atom harry.
----
Is is_list(Tail) true or false where
[_|Tail] = [harry, had, a, heap, ‘of’, apples].
True,
because Tail would be the list [had, a, heap, ‘of’, apples].
----
Is is_list(Tail) true or false where
[_ |Tail] = [harry].
True,
because Tail would be [] which is an empty list.
----
Is is_list(Head) true or false where
[ _ | [ Head | _ ] ] = [these, arms, of, mine].
False,
because the Head of the [_|Tail] would be the atom arms.
----
Is is_list(Head) true or false where
[ _ | [ Head | _ ] ] = [swing, [low, sweet], cherry, oat].
True,
because the Head of [_|Tail] would be the list [low, sweet].
----
Is it true or false that is_atom() and is_list() are BIFs?
True,
BIF stands for Built-In Function.
----