Basic element operations in Erlang summary _erlang

Source: Internet
Author: User
Tags anonymous generator

In Erlang shell, end an expression with a period with a space, tab, or carriage return,% to indicate the starting point of the comment, and the quarantine clause. The module is a. erl file, the header file of the library. HRL, compile-time C () in the shell, ERLC when the command is compiled outside, exit the shell with Q (), or Erlang:halt ().

Variables begin with uppercase letters and cannot be rebind, and can only be assigned at once, with immutable states. Atoms are global and do not require macro definitions or include files, start with lowercase letters, and enclose them in single quotes, which are minimalist expressions.

A tuple (tuple) is a number of fixed items that are grouped together into a single entity {,}, which is usually labeled on the first element because it is anonymous, to increase readability. Extract the values in a tuple using the pattern matching operator =,_ is an anonymous variable, and multiple _ does not have to be bound to the same value. However, _mode is a general variable. For example:

Copy Code code as follows:

2> Family={family,father,mother,son}.
{Family,father,mother,son}
3> {_,x,y,z}=family.
{Family,father,mother,son}
4> X.
Father
5> Y.
Mother
6> Z.
Son

A record is another form of a tuple that can be associated with a name for each element of a tuple. Use a record case:
1 using some predetermined and fixed number of atoms to represent the data
2 element number and element name will not change over time
3) Each tuple in the Daewon group has the same structure
Use #myrecord{k1=v1,k2=v2.} To create a record.

Lists (list), like [,,] can store any number of things. The head can be anything, and tail is usually still a list. Just use [... | T] to build a list, you should make sure T is a list. Also use pattern matching to extract elements from the list. General form of List derivation:
[X| | Qualifier1,qualifier2,...]
X is any expression, qualifier qualifier can be generator, bit string generator or filter. How the generator is written
pattern<-listexpr
When you invert a list, you invoke Lists:reverse.

There is no string in Erlang, the string is an integer list, and "Hellocloud" is a shorthand for a list, Io:format to specify the printout.

A mapping group (map) is an associative set of key-value pairs, stored internally as an ordered plan, and applies to situations:
1 key is not predictable when the key value to the data structure
2 There are a large number of different keys to represent the data
3 The universal data structure when the efficiency is not important
4) Self-explanatory data structure
5) used to represent a key-value parse tree, such as XML or configuration file
6 using JSON to communicate

Syntax for mapping groups:

Copy Code code as follows:

#{key1 op val1,key2 op val2,..., keyn op valn}

#后没有名称, OP is => or: = one. => updates an existing key value to a new value or adds a new key value pair to the mapping group. : = for update. The health cannot contain any variables, and values can contain unbound variables that are bound after the pattern match succeeds.

The mapping group can be exported through the ~P option in Io:format and read in Io:read or File:consult.

Copy Code code as follows:

Maps:to_json (MAP)->bin into binary JSON
Maps:from_json (BIN)-> Nap, converting binary JSON to map

The corresponding relationship between JSON and the mapping group:

1 JSON number: integer or floating-point numbers for Erlang
2 JSON string: Erlang binary
3 JSON list: Erlang list
4) True and false correspond
5 The health of the mapping group must be atomic, string or binary, and the value must be represented by the data type of JSON

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.