SoFunction
Updated on 2025-04-08

Summary of basic elements in Erlang

In Erlang shell, the expression is ended with a period plus a space, a tab or a carriage return, % represents the starting point of the comment, and isolate the clause. The module is a .erl file, the library's header file.hrl, the shell compile time c(), and erlc when compiling commands outside the shell, use q() to exit the shell, or erlang:halt().

Variables start with capital letters and cannot be rebinded. They can only be assigned values ​​at one time and have an immutable state. Atoms are global, do not require macro definitions or include files, start with lowercase letters, and can also be placed in single quotes, which are minimal expressions.
 
Tuples are some fixed number of items that form a single entity {,}. Since they are anonymous, they are usually labeled on the first element to increase readability. Extracting values ​​in tuples uses the pattern matching operator =,_ is an anonymous variable, and multiple_ does not have to bind the same value. However, _Mode is a regular variable. For example:

Copy the codeThe code is as follows:

2> Family={family,father,mother,son}.
{family,father,mother,son}
3> {_,X,Y,Z}=Family.
{family,father,mother,son}
4> X.
father
5> Y.
mother
6> Z.
Son

Record is another form of a tuple, and each element of a tuple can be associated with a name. Scenarios for using records:
1) Use some pre-determined and fixed number of atoms to represent data
2) The number of elements and name of the element will not change over time
3) Each tuple in a large tuple has the same structure
Use #myrecord{k1=v1,k2=v2..} to create a record.

A list is like [,] and can store any number of things. Head can be anything, and Tail is usually still a list. As long as you build a list with […|T], make sure T is a list. Also use pattern matching to extract elements in the list. The regular form of list comprehension:
[X||Qualifier1,Qualifier2,…]
X is any expression, and the qualifier can be a generator, a bit string generator, or a filter. How to write a generator
Pattern<- ListExpr
When reversing a list, call lists:reverse.
 
There are no strings in Erlang, strings are a list of integers, "HelloCloud" is the abbreviation of a list, io:format specifies printout.
 
Map groups (map) are the associative sets of key-value pairs, stored internally as an ordered plan, applicable situations:
1) When the key cannot be predicted, it represents the key-value pair data structure.
2) There are a large number of different keys to represent data
3) Universal data structure when efficiency is not important
4) Self-interpreted data structure
5) Used to represent key-value parsing trees, such as xml or configuration files
6) Use Json to communicate

Syntax for mapping groups:

Copy the codeThe code is as follows:

#{key1 op val1,key2 op val2,…,KeyN op valN}

There is no name after #, op is one of => or:=. => Update an existing key value to a new value or add a new key value pair to the map group. := for update. The key cannot contain any variables, and the value can contain unbound variables, which are bound after the pattern matching is successful.

The mapping group can be output through the ~p option in io:format and read with io:read or file:consult.

Copy the codeThe code is as follows:

Maps:to_json(Map)->Bin is converted into binary json
Maps: from_json(bin)-> Nap, convert binary json into map

Correspondence between Json and mapping group:

1) Json number: Erlang's integer or floating point number
2) Json string: Erlang binary type
3) Json list: Erlang list
4) Corresponding to true and false
5) The key in the mapping group must be atomic, string or binary type, and the value must be represented by the JSON data type