Erlang Grammar Learning Notes: variables, atoms, tuples, lists, strings _erlang

Source: Internet
Author: User
Tags anonymous garbage collection lowercase printable characters

One: Variable

1. The variable "single assignment" in Erlang means that the variable can only be assigned one time.
2. Variables must begin with a "capital letter".

Two: Atoms

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

Atoms are a string that starts with "lowercase letters," but if you use single quotes, the name of the atom doesn't matter, and a lot of the tricks come out. If the first character is a lowercase letter on the right track, the value of the atom is within quotation marks, otherwise the output value contains single quotes.

Three: Meta Group (tuple)

There are a number of items that make up a single entity, similar to the structure in C + +, in Erlang;

Enclosing a number of values separated by commas in curly braces is a tuple.

{rex,1.71} This tuple contains an atom and a floating-point value. Tuples are anonymous in comparison to the structure in C + +, but Erlang cannot operate with dots, and creating a tuple is binding a tuple directly to a variable, because the elements in the tuple have no names, so we can only remember the usefulness of those elements. Usually we use an atom as the first element of a tuple to indicate what this tuple represents.

{point,10,45} to replace {10,45}, which is a programmatic style.

Tuples can be nested. Such as:

Copy Code code as follows:

Person={person,

{Name,rex},

{height,1.72},

{footsize,43},

{Eyecolor,brown}}.

Represents the information of a group of people, noting that atoms are used as labels.

The tuple declaration is automatically created and destroyed with no use, with garbage collection.

The new tuple refers to a bound variable, and it has the data structure referenced by that variable. There is an error in applying a variable that is not defined!

Example:

Copy Code code as follows:

F = {Firstname,rex}.

L = {Lastname,yuan}.

P = {person,f,l}.

The value of P is {Person,{firstname,rex},{lastname,yuan}}

= is not an assignment, is a pattern match, is the basis of Erlang.

If you want to extract content from a tuple:

Declare a tuple of the same type, the value to be taken instead of the variable, OH!!! Then use = pattern matching to take out the corresponding variable value

For example:

Copy Code code as follows:

Point = {point,10,43}.

{Point,x,y} = point.


The values for x and Y are respectively 10,43.

The tuples on either side of the equals sign must contain the same number of elements, paying attention to pattern matching. If there are complex tuples to extract content, you can use the same structure of the pattern to extract, you need to extract the field location must use unbound variables, remember!! Those that are not interested can be replaced by the placeholder "_", which is an anonymous variable. In different places in the same pattern, the values of the placeholder bindings do not have to be the same.

Four: List

1: Enclose a number of values separated by commas in a square bracket is a list, and notice the difference between the tuples.

Tuples are in curly braces, and the list is in square brackets.

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

The first element of the list is called the head of the list, the rest is the end of the list (tail), and the head of the list can be anything, but the tail is usually a list. Access to the list header is very efficient. Many list-processing functions are handled by the same enemy.

[] is an empty list, [h| T] is a list with H as its head and T as its tail. | You can separate the list header from the tail. It is best to ensure that T is on the right track list. Add a list of content generally used in front of the operation, you can insert a number of elements.

2: Extract list elements

Extracted on the basis of pattern matching.

Five: string

There is no string in Erlang, and the string is actually a list of integers.

Enclosed in double quotes is a string.

Copy Code code as follows:

Name= "Rex"

You must use double quotes. When the shell prints a list of values, the list is treated as a string only if all the integers in the list are printable characters. There is no one can not.

You can use the "$" symbol to represent the integer value of the character, in fact, ASCII, $s is 115,

Note the character set, confirm the display terminal and locale problem, on this issue Erlang has no way to 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.