Erlang Grammar Learning Notes: Variables, Atoms, Tuples, Lists, Strings


1: Variables

1. Variables in Erlang [single assignment] mean that variables can only be assigned once.
2. Variables must begin with capital letters.

Two: Atoms

Atoms are equivalent to enumeration types in c++, but the value of atoms in Erlang is itself.

Atoms begin with a bunch of lower-case letters, but if they are given a single quotation mark, the name of the atom doesn’t matter. A lot of tricks come out. If the first character is a regular lowercase letter, the atomic value is the content of quotation marks, otherwise the output value contains single quotation marks.

Three: tuple

There are many items that make up a single entity, similar to the structure in c++, which is represented by tuples in erlang.

A tuple consists of several values separated by commas and enclosed in brackets.

The tuple {rex, 1.71} contains an atom and a floating point value. Tuples are anonymous compared with structures in c++, but Erlang cannot operate by dots. To create a tuple is to bind a tuple directly to a variable. Because elements in tuples have no names, we can only remember the usefulness of these elements. Usually, we use an atom as the first element of a tuple to express the meaning of the tuple.

It is a programming style to replace {point, 10,45} with {10,45}.

Tuples can be nested. For example:

Copy codeThe code is as follows:





Represents a group of people’s information. Note that atoms are used as tags.

Tuple declarations are created automatically, destroyed when they are no longer used, and garbage collection occurs.

The new tuple refers to a bound variable, which enjoys the data structure referenced by the variable. Error applying undefined variables!


Copy codeThe code is as follows:
 F = {firstname,rex}.

L = {lastname,yuan}.

P = {person,F,L}.

The value of P is {person, {first name, rex}, {last name, yuan}.

= It’s not assignment, it’s pattern matching, it’s the basis of erlang.

If you want to extract content from tuples:

Declare a tuple of the same type, the value to be taken is replaced by a variable, variable oh!!! Then use = pattern matching to extract the value of the corresponding variable

For example:

Copy 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 an equal sign must contain the same number of elements, paying attention to pattern matching. If you want to extract content from a complex tuple, you can use the same structure pattern to extract the location of the field you need to extract, you must use unbound variables, remember!! Uninterested can be replaced by placeholder “”, which is an anonymous variable. In different places in the same schema, placeholder binding values need not be the same.

Four: List

1: Wrap up a list of values separated by commas with one square bracket. Notice the difference between them and tuples.

Tuples are bracketed and lists are bracketed.

The elements in the list can be of 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 usually a list. Accessing list headers is very efficient. Many list handlers start processing on the head.

[] is an empty list, [H | T] is a list with H as its head and T as its tail. “|” separates the head and tail of the list. It’s best to make sure that T is on the right track. Adding list content is usually done by pre-insertion, which can insert many elements at a time.

2: Extract list elements

Extraction is based on pattern matching.

Five: Strings

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

A string is enclosed in double quotation marks.

Copy codeThe code is as follows:

Double quotation marks must be used. When a shell prints a list of values, it treats the list as a string only if all integers in the list are printable characters. One is not enough.

You can use the “$” symbol to represent the integer value of the character, which is actually ASCII code, $s is 115.

Note the character set, confirm the display terminal and region settings, Erlang has no way to solve the scrambling problem on this issue.