As stated in "The Future of Code", in order to make full use of multi-cores, concurrency will become a trend in the future development, Erlang is indeed the best choice for the support of concurrent programming. Erlang is still relatively niche in China and has relatively few classic books. Finally, he chose "Erlang Programming 2nd Edition" as my first introductory book of Erlang.
Erlang official website:/
Erlang installation:
For Ubuntu and Debian distributions, you can use: apt-get install erlang to install;
For other UNIX and Linux platforms, you can download the source code and build it manually, as follows:
Download:/
1. Unzip the source code: $tar zxf otp_src_17.
2. $ cd opt_src_17.3
3. $ ./configure
4. $ make
5. $ make install
Erlang is a functional programming language designed mainly to solve bottom-up writing problems such as concurrent, distributed, fault-tolerant, scalable and soft real-time systems.
Since I have never been involved in functional programming before, I should start step by step from simple sequential programming.
It is very simple to enter the shell programming environment of Erlang. Similar to Python, you can just enter erl directly in the terminal. If you exit, you can use Ctrl+c and then enter a method, but this may lead to some data destruction, so it is best to use q().
Comments: %
variable:All variable names must start with capital letters. Once the variable is assigned, it is not allowed to change.
Pattern matching operation:
Lhs = RhsIt means to calculate the value on the right and then match the result to the pattern on the left;
X = Expression. If X is not assigned before, the match will definitely be successful. X = AnotherExpression. The match will only be successful when Expression = AnotherExpression, otherwise the match will fail;
Floating point number:
When using "/" to divide two integers, the result will be automatically converted to a floating point number;
To get integers from division, div and rem must be used; for example: 5 div 3. The result is 1, 5 rem 3. The result is 2;
atom:
Atoms are used to represent constant values, and are global. Atoms start with lowercase letters and are followed by a string of letters, numbers, underscores or @ symbols;
Atoms can be placed in single quotes, and atoms that start with capital letters or contain characters other than alphanumeric characters, such as: ‘Monday’, ‘+’, ‘a test paper’, etc. The value of the atom is itself;
Tuple:
For easy understanding, it is best to add tags to tuples, for example: {point, 10, 45}.
Create a tuple: F = {firstName, joe}. L = {lastName, Tom}. P = {person, F, L}.
Extract firstName: {_, {_, Who}, {_, _}} = P.
List:
Each element in the list can be of any type. In [H|T], H is the head of the list and T is the end of the list, for example:
L = [{name, tom}, 1, 2.0, test, {point, 10, 23}].
[H|T] = L.
Then, H = {name, tom}, T = [1, 2.0, test, {point, 10, 23}].
String:
A string literal is a string of characters surrounded by double quotes, for example:
N = "Hello". "Hello" is actually just abbreviation for a list, which contains integer codes representing each character in the string.
X = [97, 98, 99] .
Print out "abc".
io:format("~w~n", ["abc"]).
Print out [97, 98, 99]
[H|T] = "cat". H = 99, T = "at".
f() makes the shell forget any existing bindings.