Erlang functions and Process Control [2]

Source: Internet
Author: User
Document directory
  • If

1: Case statement

The case expression allows you to select multiple options within the clause body. The syntax is as follows:

Case expr
Pattern1 [When guard1]-> seq1;
Pattern2 [When guard2]-> seq2;
...
Patternn [When guardn]-> seqn
End

First, evaluate the value of expr. Then, the value of expr will be in sequence with pattern1, pattern2 ...... Patternn matches until the match is successful. If a matching and (optional) Protective formula is found, the corresponding call sequence is evaluated. Note that the case protection and function protection are the same. The value of the Case primitive is the value of the selected sequence.

At least one pattern must be matched. -- otherwise, a runtime error will occur and ?? Chapter.

For example, we have a function named allocate (Resource) to allocate a resource. Suppose this function only returns {Yes, address} or no. In this way, this function can be placed in a case structure:

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

In sequence 1..., the variable address has been bound to the returned results of allocate/1.

To avoid Matching errors, we often append a pattern that must be matched as the last branch of the Case primitive:

Case FN
...
_->
True
End

If

The syntax of the IF expression is as follows:

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

In this case, the Protection Type guard1,... will be evaluated in sequence. If a protection formula is set, evaluate the sequence associated with it. The result of this sequence is the result of the IF structure. If and function are the same. Similar to case, if a protection method is not set, an error is thrown. If needed, you can add a protective asserted true as a waste bin:

If
...
True->
True
End

Arithmetic expression

An arithmetic expression is composed of the following operators:

Operator Description Type Operand type Priority
+
X
+
X
Single Camera Hybrid 1
-
X
-
X
Single Camera Hybrid 1
X
*
Y
X
*
Y
Binocular Hybrid 2
X
/
Y
X
/
Y

(Floating point Division)
Binocular Hybrid 2
X
Div
Y
X

DivisionY
Binocular Integer 2
X
Rem
Y
X

DividedY

Remainder
Binocular Integer 2
X
Band
Y
X

AndY

Bit and
Binocular Integer 2
X
+
Y
X
+
Y
Binocular Hybrid 3
X
-
Y
X
-
Y
Binocular Hybrid 3
X
Bor
Y
X

AndY

Bit or
Binocular Integer 3
X
Bxor
Y
X

AndY

Bitwise arithmetic or
Binocular Integer 3
X
BSL
N
X

Shift leftN

Bit
Binocular Integer 3
X
BSR
N
X

Right ShiftN

Bit
Binocular Integer 3

Single Camera
The operator has a parameter,Binocular
The operator has two parameters.Hybrid
This means that the parameter can beInteger

Or yes.Float

. The return value of a single object operator is of the same type as its parameter.

Binary mixed operators (that is*

,-

,+

)Integer

The return type isInteger

Contains at least oneFloat

ReturnsFloat

. Floating Point division Operator/

Always ReturnsFloat

.

Binary integer operator (that isBand

,Div

,Rem

,Bor

,Bxor

,BSL

,BSR

.

The order of evaluation depends on the operator's priority. First, the operator that calculates the 1st priority, then the 2nd priority, and so on. Evaluate the expressions in parentheses first.

Operators with the same priority 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.