71
|
1 Install Erlang with "sudo zypper install erlang"
|
|
2
|
|
3 Erlang has a REPL (erl)
|
|
4
|
|
5 Comments are "%", just to be different
|
|
6
|
|
7 Commands need to end with a full-stop (like Prolog)
|
|
8
|
|
9 Erlang has dynamic typing with coercion: 2 + 2 = 4, 2 + 2.0 = 4.0
|
|
10 → BUT lists aren't just lists, they're strings when the values are printable ASCII 😮
|
|
11 → AND numbers aren't coerced to strings
|
|
12
|
|
13 As well as strings there are "atoms" - arbitrarily named symbols.
|
|
14
|
|
15 Variables start with upper-case.
|
|
16
|
|
17 Variables can't be redefined. Trying results in "exception error: no match of right hand side value <the value>"
|
|
18
|
|
19 Lists use square brackets. Tuples use curly braces.
|
|
20
|
|
21 Tuples are apparently used instead of maps and hashes to associate atoms as keys with values.
|
|
22 → Values are extracted from tuples using pattern matching
|
|
23 → BUT this implies matching ordering (which isn't the same as maps)
|
|
24
|
|
25 Example:
|
|
26 Person = {person, {name, "Agent Smith"}, {profession, "Killing programs"}}.
|
|
27 % person, name and profession are atoms
|
|
28 {person, {name, Name}, {profession, Profession}} = Person.
|
|
29 Name. % prints "Agent Smith"
|
|
30 Profession. % prints "Killing programs"
|
|
31
|
|
32 The initial atom is an Erlang-ism to make it easier to match all "person" tuples
|
|
33
|
|
34 Erlang can pack and unpack bits using "<<Variable:bit_count[ Variable:bit_count[, …]]>>" and then pattern matching to unpack
|
|
35
|
72
|
36 Functions are well documented at http://erlang.org/doc/apps/stdlib/index.html
|
|
37
|
|
38 Case statements use patern matching:
|
|
39
|
|
40 Animal = "dog".
|
|
41 case Animal of
|
|
42 "dog" -> underdog;
|
|
43 "cat" -> thundercat;
|
|
44 _ -> something_else
|
|
45 end.
|
|
46
|
|
47 If statements use "guards" (conditions) and implies "elseif". Else requires a guard of "true".
|
|
48
|
|
49 if
|
|
50 X > 0 -> positive;
|
|
51 X < 0 -> negative;
|
|
52 true -> zero
|
|
53 end.
|
|
54
|
|
55 Anonymous functions are defined as "fun(Var[, …]) -> [code] end."
|
|
56
|
|
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.
|
|
58 → There are also map, filter, all and any functions, etc.
|
|
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
|
|
60
|
|
61 Erlang has list comprehension - similar to Python's, but with Erlang-y syntax.
|
|
62 → [Function(Val) || Val <- List]. % Call Function on Val for each Val in List
|
|
63 → Can also be done without a function and just code - e.g. [X * 2 || X <- [1, 2, 3]].
|
|
64 → Therefore, map(Func,List) can be defined as [ Func(X) || X <- List ].
|
|
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)
|
|
66 |