Erlang basic syntax learning notes (1)

Source: Internet
Author: User

I. Variables

1. The variable [Single Assignment] in Erlang means that the variable can be assigned only once.

2. The variable must start with an uppercase letter.

Ii. Atom

The atom is equivalent to the enumeration type in C ++, but the value of the atom in Erlang is itself.

An atom is a string starting with a lower-case letter. However, if it is enclosed by single quotes, it doesn't matter if it is named. A lot of tricks come out. If the first character is the right lowercase letter, the atomic value is the content in the quotation marks. Otherwise, the output value contains single quotation marks.

 

3. tuple)

There are many items to form a single entity, similar to the struct in C ++, which should be represented by tuples in Erlang;

Enclose several values separated by commas (,) in curly brackets as a tuples.

{Rex, 1.71} The tuples contain an atom and a floating point value. The structure of tuples is anonymous in comparison with that of C ++, but Erlang cannot be operated by periods. To create a tuple, bind a variable to a single tuple directly, because the elements in the tuples have no names, we can only remember the usefulness of these elements. In general, an atom is used as the first element of the tuples to indicate the meaning of the tuples.

{Point,} is replaced by {}, which is a programming style.

Tuples can be nested. For example:

Person = {person,

{Name, Rex },

{Size, 1.72 },

{Footsize, 43 },

{Eyecolor, brown }}.

Indicates the information of a group of people. Note that atoms are used as tags.

The tuples are automatically created. If they are no longer used, they are destroyed and garbage collection occurs.

If a new Meta Group references a bound variable, it will enjoy the data structure referenced by this variable. An error occurs when no variables are defined in the application!

Example:

F = {firstname, Rex }.

L = {lastname, Yuan }.

P = {person, F, L }.

The value of P is {person, {firstname, Rex}, {lastname, Yuan }}

= It is not a value assignment, it is a pattern match, and it is the basis of Erlang.

If you want to extract content from the tuples:

Declare a tuples of the same type. Replace the values with variables !!! Then use the = pattern matching to obtain the value of the corresponding variable.

For example:

Point = {point, 10, 43 }.

{Point, x, y} = point.

The values of X and Y are respectively.

The tuples on both sides of the equal sign must contain the same number of elements. Pay attention to pattern matching. If there are complex tuples that want to extract content, you can use the same structure to extract the content. The field location to be extracted must use unbound variables. Remember !! If you are not interested, you can use the Placeholder "_" as an anonymous variable. In the same mode, the placeholder value does not have to be the same.

Iv. List

1: Enclose several values separated by commas (,) in one square brackets as a list. Note the differences between the values and the element groups.

The tuples are curly braces, and the list is square brackets.

The elements in the list can have different types. Example: [1 + 2, hello, 2-2, {cost, apple, 30-20}, 3]

The first element of the list is the header of the list. All the remaining elements are the tail of the list. The header of the list can be anything, but the end is usually a list. The access list header is very efficient. Many list processing functions start to process each other.

[] Is an empty list. [H | T] is a list starting with H and ending with T. "|" Can be used to separate the header and tail of the list. It is best to ensure that t is on track. You can insert multiple elements to the list.

2: extract list elements

It is extracted based on pattern matching.

5. String

There is no string in Erlang, and the string is actually an integer list.

Enclosed in double quotation marks is a string.

Name = "Rex"

Double quotation marks are required. When shell prints a list value, the list is treated 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 of a character, which is actually an ascii code, and $ S is 115,

Pay attention to the character set and confirm the display terminal and region settings. On this issue, Erlang cannot solve the garbled problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.