SoFunction
Updated on 2025-04-08

Erlang syntax learning notes: variables, atoms, tuples, lists, strings

One: Variable

1. The variable in erlang [single assignment] means that the variable can only be assigned once.
2. The variable must start with [capsular letters].

Two: Atom

Atoms are equivalent to the enum type in c++, but the value of the atom in erlang is itself.

Atoms start with [lowercase letters], but if they are enumerated in single quotes, it doesn’t matter if the atom is named, and many tricks will come out. If the first character is a lowercase letter in the right track, the value of the atom is in the quotes, otherwise the output value contains single quotes.

Three: Tuple

There are many items that form a single entity, similar to structures in C++, and should be represented by tuples in erlang;

Enclosing several values ​​separated by [comma] in [braces] is a tuple.

{rex,1.71} This tuple contains an atom and a floating point value. Tuples are anonymous compared to the structures in C++, but they cannot be operated with dots in erlang. Creating a tuple is to directly bind a tuple to a variable. Because the elements in the tuple have no name, we can only remember the usefulness of these elements. Usually, we use an atom as the first element of the tuple to indicate the meaning of the tuple.

{point,10,45} instead of {10,45}, which is a programming style.

Tuples can be nested. for example:

Copy the codeThe code is as follows:

Person={person,

                          {name,rex},

                          {height,1.72},

                          {footsize,43},

                           {eyecolor,brown}}.

To represent information about a group of people, note that using atoms as labels.

Tuple declarations are automatically created and will be destroyed if they are no longer used. There will be garbage collection.

A new tuple refers to a bound variable, and then it will enjoy the data structure referenced by this variable. An error occurs when applying undefined variables!

example:

Copy the codeThe code is as follows:

 F = {firstname,rex}.

L = {lastname,yuan}.

P = {person,F,L}.

The value of p is {person,{firstname,rex},{lastname,yuan}}

= is not assignment, it is pattern matching, and it is the basis of erlang.

If you want to extract content from a tuple:

Declare a tuple of the same type, and replace the value to be taken with a variable, variable! ! ! Then use = pattern matching to get the corresponding variable value

For example:

Copy the codeThe code is as follows:

Point = {point,10,43}.

{point,X,Y} = Point.


The values ​​of X and Y are 10 and 43 respectively.

Tuples on both sides of the equal sign must contain the same number of elements, pay attention to pattern matching. If you have complex tuples that want to extract content, you can use the same structure pattern to extract it. The field positions that need to be extracted must be used with unbound variables. Remember! ! Those who are not interested can use the placeholder "_" instead, which is an anonymous variable. The value of placeholder binding does not have to be the same in different places in the same pattern.

Four: List

1: Enclose several values ​​separated by [comma] in a [square bracket] to make a list. Pay attention to the difference between them and tuples.

Tuples are curly braces, and lists are square braces.

Elements in the list can have their own different types. For example: [1+2,hello,2-2,{cost,apple,30-20},3]

The first element of a list is called the head of the list, and the rest is the tail of the list. The head of the list can be anything, but the tail is generally still a list. Accessing list headers is very efficient. Many list processing functions start with the header.

[] is an empty list, [H|T] is a list with H as the header and T as the tail. "|" can separate the list's head and tail. It is best to ensure that T is the right track list. Adding list content is generally done by inserting it before, and you can insert many elements at once.

2: Extract list elements

Extract based on pattern matching.

Five: String

There are no strings in erlang, strings are actually a list of integers.

Enclosed in double quotes is a string.

Copy the codeThe code is as follows:

Name=“Rex”

Double quotes must be used. When a shell prints a string of list values, the list will be regarded as a string only when all integers in the list are printable characters. If there is one, it won’t work.

The "$" symbol can be used to represent the integer value of a character, which is actually ASCII code, and $s is 115.

Pay attention to the character set and confirm the display terminal and area setting problem. On this issue, erlang has no way to solve the garbled problem.