Introduction to functions and process control in Erlang _erlang

Source: Internet
Author: User
Tags arithmetic case statement comparison function definition mixed

One: function

1: In Erlang, the two functions with the same name but different number of arguments are completely different functions.

2: Functions within other modules are invoked with fully qualified names:

Copy Code code as follows:

-module (SORT1).
-export ([REVERSE_SORT/1, SORT/1]).

Reverse_sort (L)->
Lists1:reverse (sort (L)).
Sort (L)->
Lists:sort (L).


3: The clause is separated by a semicolon ";" and ends with "." At the end of the final sentence.

4: Each function consists of a set of clauses. The clause is separated by a semicolon ";" Separated. Each clause contains a clause header, an optional protection, and a clause body. The header of a clause contains a function name and a comma-delimited set of arguments that, when called, will match the headers in the function definition sequentially. All assertions are evaluated when evaluated against the protected value. If all assertions are true, the protection is established, otherwise it will fail. The order of evaluation for each assertion in the protection is indeterminate.

If the protection is established, the body of the clause is evaluated. If the protection fails, the next candidate clause is attempted. Once the header and protection of the clause are matched successfully, the system will specify the note and evaluate its body. The combination of a clause header pattern and a protected type can uniquely determine a correct clause.

The complete set of protected assertions is as follows:

Protection Type Conditions of Establishment
Atom (X) X is an atomic type
Constant (X) X is not a list or tuple
Float (X) X is a floating-point number
Integer (X) X is an integer
List (X) X is a list or []
Number X is an integer or floating-point number
PID (X) X is a process identifier
Port (X) X is a port
Reference (X) X is a reference
Tuple (X) X is a tuple
Binary (X) X is a piece of binary data

In addition, some combinations of bif and arithmetic expressions can also be used as protection. They are:

Copy Code code as follows:

ELEMENT/2, FLOAT/1, HD/1, LENGTH/1, ROUND/1, self/0, ZE/1
TRUNC/1, TL/1, ABS/1, NODE/1, node/0, nodes/0

The item comparison operators that can appear in the protection are as follows:

operator Description type
X > Y X is greater than Y Coerce
X < Y X is less than Y Coerce
X =< Y X is less than or equal to Y Coerce
X >= Y X is greater than or equal to Y Coerce
X = = Y X equals y Coerce
X/= Y X is not equal to Y Coerce
X =:= Y X equals y Exact
X =/= Y X is not equal to Y Exact

The comparison operator works as follows: First evaluate both sides of the operator (for example, when an arithmetic expression exists on either side of the expression or when the Bif protection function is included), and then compare.

For comparison purposes, define the following partial order relationship:

Copy Code code as follows:

Number < Atom < reference < port < pid < tuple < list

Tuples are sorted first by size and then by element. The list is compared in order of first head, rear tail.

If the two parameters of the comparison operator are numeric and the operator is coerce, if one argument is integer and the other is float, then the integer is converted to float and then compared.

Operators of the exact type do not make such a conversion.

So 5.0 = = 1 + 4 is true, and 5.0 =:= 4 + 1 is false.
Example of a protection function clause:

Copy Code code as follows:

Foo (x, Y, Z) when integer (x), Integer (y), Integer (Z), X = = Y + Z->
Foo (x, Y, z) when list (x), HD (x) = = {Y, length (z)}->
Foo (x, y, Z) when {x, y, size (z)} = = {A, x}->
Foo (x) when list (x), HD (x) = = C1, HD (TL (x)) = = C2->

Note that you cannot introduce new variables in a protected type.

Second, Process Control

Case statement

Case expressions allow you to select from multiple options within a clause body, as follows:

Copy Code code as follows:

Case Expr of
PATTERN1 [when Guard1]-> Seq1;
Pattern2 [when Guard2]-> Seq2;
...
PATTERNN [when Guardn]-> seqn
End

First, evaluate expr, and then the value of expr is followed by the pattern Pattern1, Pattern2 ... Patternn to match until the match is successful. If a matching and (optionally) protected form is found, the corresponding call sequence is evaluated. Note that case protection is the same as the form of function protection. The value of the case primitive is the value of the selected sequence.

At least one pattern must be matched-otherwise a run-time error will be generated and the first. The error-handling mechanism in the chapter.

For example, let's say we have a function allocate (Resource) for allocating a resource Resource. Suppose this function only returns {yes, address}, or No. In this way, this function can be placed in a case structure:

Copy Code code as follows:

...
Case allocate (Resource) of
{yes,address} When address > 0, address =< Max->
Sequence 1 ...;
No->
Sequence 2 ...
End
...

In sequence 1 ... , the variable address is already bound to the return result of the ALLOCATE/1.

To avoid matching errors, we often append a pattern that must match to the last branch of the case's primitives:

Copy Code code as follows:

Case Fn of
...
_->
True
End

IF

The syntax for an if expression is as follows:

Copy Code code as follows:

If
Guard1->
Sequence1;
Guard2->
Sequence2;
...
End

In this case, the protective type of Guard1,... will be evaluated in turn. If a protective form is established, the sequence to which it is associated is evaluated. The result of the evaluation of the sequence is the result of the IF structure. The If protection is the same as the form of the function protection. As with the case, an error will be raised if none of the protections is valid. If you want, you can increase the protection assertion true as a trash bin:
Copy Code code as follows:

If
...
True->
True
End

An arithmetic expression

Arithmetic expressions consist of the following operators:

operator Description type type of operand Priority Level
+ X + X Monocular Mixed 1
-X -X Monocular Mixed 1
X * Y X * Y Binocular Mixed 2
x/y x/y (floating-point division) Binocular Mixed 2
X Div Y X Divide y Binocular Integer 2
X REM Y The remainder of X divided by y Binocular Integer 2
X band Y The bit of X and Y Binocular Integer 2
X + Y X + Y Binocular Mixed 3
X-y X-y Binocular Mixed 3
X Bor Y X and y bit or Binocular Integer 3
X Bxor Y The digits of X and y differ or Binocular Integer 3
X BSL N X arithmetic left n bit Binocular Integer 3
X BSR N X Right Move n bit Binocular Integer 3

The monocular operator has one parameter, and the binocular operator has two parameters. Mixing means that the parameter can be an integer or float. The return value of the monocular operator is the same as its parameter type.

The binocular mixing operator (that is, * 、-、 +) Returns an object of type integer when the parameter is integer, and returns a float when the parameter contains at least one float. The floating-point division operator/always returns a float.

The parameters of the binocular integer operator (i.e. band, Div, REM, Bor, Bxor, BSL, BSR) must be integers, and their return values are integers.

The order of evaluation depends on the precedence of the operator: first compute the 1th precedence operator, then the 2nd priority, and so on. Expressions in parentheses are preferred.

Operators with the same precedence are evaluated from left to right.

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.