Erlang Basics-Data types

Source: Internet
Author: User

Erlang data types are very small and easy to master:

First, numeric types (integers and floating-point numbers)

1, Integer

There is no limit to the size of integers, smaller integers are stored in a single machine word length, and larger integers are automatically allocated memory. So there is no overflow problem at all.

Example:

101

-101

1234567890 * 987654321 * 999999999

In addition, you can use 2 binary to 36 binary integer, such as: 16#ff00,2#10101,36#zz.

Also, the value of any character can be encoded by using the $ prefix notation (ascii/latin-1/unicode)

such as: $9, $z, $\n

2. Floating point number

Erlang's floating-point numbers are double-precision, with no single-precision argument, and floating-point numbers cannot be used. (full stop), such as:. 01, while Erlang requires a number to start with, such as: 0.01.

3, arithmetic operators and bitwise operations

+,-and *

Where the division divides into two kinds:/and Div, such as: 4/2 the result is 2.0,4 Div 2 result is 3, the remainder of the integer operator is implemented by the operator REM, such as: The result is 3 REM 4.

There are also operators for integer-bit operations: N BSL K means that the integer n is shifted to the left K-bit, and BSR is shifted to the right. The bitwise logical operators are band, Bor, Bxor, and Bnot, respectively. For example: X band (Bnot y) will zero the bits occupied by Y in X again.

Two binary strings and bit strings

Integer value range 0~255, example: <<0,1,2,..., 255>> You can also construct a binary string with strings, example:<< "Hello", "World" >>

Third, the Atom

Erlang a special string constant that is only identified by a sequence of characters, so that two atoms are identical as long as they have the same character. But inside the system, these strings are stored in a table and positioned by the table's subscript, so you can determine whether two atoms are equal at run time by comparing two small integers. Each atom occupies only one word length of memory.

In Erlang, an atom acts like an Enum constant in Java or C, except that it is free to create and use it anywhere without having to declare the atom beforehand. Typically, an atom begins with a lowercase letter, such as:

OK, error, foo, undefined

If you use any of the other special characters, you have to enclose them in single quotes, such as:

' ¥%#*! ’

' Blanks and Capitals can be quoted '

You should think of atoms as a special kind of label, not as a normal string. Their maximum length is 255 characters, and the total number of atoms in a single system is also capped: At present it is distances, exactly 1,048,576. In general, this limit is large enough, but for a long-running system, you should avoid dynamically generating globally unique atoms such as ' x_4711 ', ' x_4712 '. Atoms have been created and will never be purged, even if they are no longer in use, unless the system restarts.

Four, the tuple

Tuples are fixed-length ordered sequences, and tuples are represented by braces, such as:

{1, 2, 3}

{One, one, three, four}

{from, ' Russia ', ' With Love '}

{complex, {nested, ' structure ', {here}}}

As in C, a struct or an object in Java, a tuple is the primary means of constructing a conforming data structure in Erlang or returning multiple result values at once; only the tuple's element item has no name, only the number. Accessing elements in tuples is a time-complexity operation in Changshu, which is as fast and secure as accessing array elements in Java. With the following upcoming record syntax, you can name the element in the tuple so that you don't have to use the subscript directly.

In addition, pattern matching makes it easy to use variables to refer to different parts of a tuple, so it is seldom necessary to directly use subscripts to access element items.

V. List

Lists are the real main force in Erlang's various data types. Cases:

[]

[1, 2, 3]

[One, both, three]

[{tomorrow, "buy Cheese"},

{soon, "fix Trap Door"},

{Later, "Repair Moon Rocket"}]

Add a list, such as:

[5, 4, 3 |  [2, 1]] This will get [5, 4, 3, 2, 1]

[1, 2, 3, 4] + + [5, 6, 7, 8] This will get [1, 2, 3, 4, 5, 6, 7, 8].

Six, string

The double quote string in Erlang actually ratione materiae a list, such as:

"ABCD"

"Hello!"

"\t\r\n"

Vii. unique identifiers: PID, ports, and references

1. PID (process identifier)

You can get the PID of the current process through self (), print the format:<0.35.0>

2. Port identifiers

Ports and processes are similar, but he can also communicate with the outside, the print format of the port: #Port <0.472>

3. References

The third type of data in this data type is a reference, and standing is called ref. Can be generated by the Make_ref () function, and the print format is #ref<0.0.0.39>. References are often used as a one-time label or cookie for various requirements to ensure uniqueness.

Eight, fun function

Erlang is called a functional language, a distinguishing feature of which is the ability to process functions like data--that is, functions can be input to other functions, or they can be the result of job search for other functions, as well as the existence of data structures for subsequent use, and so forth. Of course, a function call mechanism is also provided. In Erlang, this object that wraps a function into data is called a fun function.

Erlang Basics-Data types

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.