annotate 5-Erlang/README.txt @ 93:39084e2b8744

Add a function for word-aware text wrapping Potentially hugely inefficient because we iterate through the string character by character, but then splitting it first and iterating over words still needs to iterate over the string to know where to split.
author IBBoard <dev@ibboard.co.uk>
date Tue, 18 Jun 2019 21:05:00 +0100
parents e05701354b6e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
71
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 Install Erlang with "sudo zypper install erlang"
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 Erlang has a REPL (erl)
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 Comments are "%", just to be different
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 Commands need to end with a full-stop (like Prolog)
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 Erlang has dynamic typing with coercion: 2 + 2 = 4, 2 + 2.0 = 4.0
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 → BUT lists aren't just lists, they're strings when the values are printable ASCII 😮
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 → AND numbers aren't coerced to strings
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 As well as strings there are "atoms" - arbitrarily named symbols.
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 Variables start with upper-case.
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 Variables can't be redefined. Trying results in "exception error: no match of right hand side value <the value>"
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 Lists use square brackets. Tuples use curly braces.
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 Tuples are apparently used instead of maps and hashes to associate atoms as keys with values.
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 → Values are extracted from tuples using pattern matching
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 → BUT this implies matching ordering (which isn't the same as maps)
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 Example:
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 Person = {person, {name, "Agent Smith"}, {profession, "Killing programs"}}.
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 % person, name and profession are atoms
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 {person, {name, Name}, {profession, Profession}} = Person.
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 Name. % prints "Agent Smith"
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 Profession. % prints "Killing programs"
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 The initial atom is an Erlang-ism to make it easier to match all "person" tuples
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 Erlang can pack and unpack bits using "<<Variable:bit_count[ Variable:bit_count[, …]]>>" and then pattern matching to unpack
32f018861e36 Add Day 1 Erlang content
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35
72
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
36 Functions are well documented at http://erlang.org/doc/apps/stdlib/index.html
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
37
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
38 Case statements use patern matching:
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
39
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
40 Animal = "dog".
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
41 case Animal of
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
42 "dog" -> underdog;
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
43 "cat" -> thundercat;
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
44 _ -> something_else
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
45 end.
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
46
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
47 If statements use "guards" (conditions) and implies "elseif". Else requires a guard of "true".
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
48
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
49 if
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
50 X > 0 -> positive;
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
51 X < 0 -> negative;
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
52 true -> zero
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
53 end.
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
54
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
55 Anonymous functions are defined as "fun(Var[, …]) -> [code] end."
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
56
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
57 List processing is in the "lists" module - e.g. lists:foreach/2 takes function and list of values and calls function on each value in list.
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
58 → There are also map, filter, all and any functions, etc.
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
59 → Interesting extra functions are lists:takewhile/2 and lists:dropwhile/2, which filter or discard from the start of a list based on the function's return
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
60
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
61 Erlang has list comprehension - similar to Python's, but with Erlang-y syntax.
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
62 → [Function(Val) || Val <- List]. % Call Function on Val for each Val in List
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
63 → Can also be done without a function and just code - e.g. [X * 2 || X <- [1, 2, 3]].
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
64 → Therefore, map(Func,List) can be defined as [ Func(X) || X <- List ].
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
65 → EXCEPT it isn't that simple - there can be multiple clauses after the double-pipe, which can be generators (X <- List) or filter clauses (X < 3)
e05701354b6e Add notes from Day 2
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
66