The module is the basic code unit of Erlang. After the erl file is compiled, it uses .beam as the extension and uses the UTF8 character set. The .erl file is shown as follows:
-module(module name, the same as the file name that stores the module)
-export([Method name/number of input parameters])
Copy the codeThe code is as follows:
Method1( {a,b,c})->a*b*c;
Mehtod2({d,e})->d-e.
There are two types of module attributes: predefined and user-defined.
The data type used in Erlang to represent functions is called fun, which is equivalent to lambda in python, and is generally used for
1) Perform the same operation on each element in the list
2) Create your own controls
3) Implement reenterable parsing code, parsing combinator or lazy evaluator
Pattern matching is the foundation of Erlang, and case and if expressions make Erlang small and consistent.
Copy the codeThe code is as follows:
case Expression of
Pattern1[ when Guard1] -> Expr-seq1;
Pattern2[when Guard2]-> Expr-seq2;
…
end
if
Guard1-> Expr_seq1;
Guard2-> Expr_seq2;
…
end
Erlang has two methods to catch exception errors. One is to encapsulate the call function that throws the exception in a try_catch expression, providing general information, and the other is to encapsulate the call in a catch expression, providing detailed stack trace information. After catching an exception, you can call erlang:get_stacktrace() to find the nearest stack information.
The introduction of binary, bit string, and bit-level pattern matching into Erlang is to simplify network programming. A binary type is a column of integers or strings placed between double less than signs and double greater than signs.
For example: 1> Mybin1 = << “ILOVE YOU”>>
Copy the codeThe code is as follows:
Term_to_bingary(Term) ->Bin Convert to binary
Binary_to_Term(Bin) ->Term Data type converted from binary to Erlang
Careful selection of macro names and Erlang code layout can minimize the semantic gap between C and Erlang. In Erlang, the smallest addressing unit is 1 bit, and the bit sequence in the bit string can be accessed directly.