"Erlang Programming" chapter III Basic Concept Learning

Source: Internet
Author: User

    1. Variable

      the variable must start with an uppercase letter. in Erlang, variables are like those in math. When associating a value with a variable, it is an assertion, that is, a statement of fact. This variable is the value, that's all.

      Two assumptions about the simple statement of x=1234.

    • First, x is not a variable, it's not a concept that you're used to in Java and C languages.

    • Second, = is not an assignment operator, but a pattern-matching operator.

      That is, "LHS = RHS" such a formula. Its first evaluates the RHS, and then the result and the left end of the LHS mode pair.

      In Erlang, the value of a variable is the result of a successful pattern match operation.

Second, the Atom

In Erlang, atoms are used to represent constants. Corresponds to the definition of a macro in the C language. The atom starts with a lowercase letter followed by a string of letters, numbers, underscores (_), or @, which can be placed in single quotation marks (') if it does not start with a lowercase letter or a symbol other than a lowercase letter. such as ' Monday ', ' _name ' and so on .

in some languages, single and double quotation marks can be used interchangeably. That's not true in Erlang. The use of single quotation marks, as mentioned above, is used to literal the string literal (string).

Three, meta-group

Tuple (tuple) is used when you want to classify a number of fixed items into a single entity. the way to create tuples is to enclose the values you want to represent with curly braces {} and separate them with commas.

to make it easy to remember the use of tuples, a common practice is to use atoms as the first element of a tuple to represent what tuples are. This method of labeling tuples is not required by the language, but is a recommended programming style.

Take a look at the following example, what does each statement mean? Why does the match not succeed at some point?

1> p = {10 , 5}.   %% the variable p is bound to a tuple {10,5}2> p = { point, 10, 5}.  %% again assigns a value to P, then the match is unsuccessful. The variable get value is the result of a successful pattern match operation. ** exception error: no match of right hand side value {point , 10,5}3> p1 = {point, 10, 5}.  %% uses a variable that also binds a tuple {point,10,5}4> { The reason point1, x, y} = p1.  %% does not match is because Point1 and  point are different atoms. An atom represents a constant. ** exception error: no match of right hand side value {point , 10,5}5> {point, x, y} = p1.   %% matches successfully, variable x, y are bound 10, 5{point,10,5}6 respectively > x.107> y.58> x= 20.      %% the value of the X binding again is unsuccessful because the variable x is bound to a value. ** exception error: no match of right hand side value 20 

Understanding the above example, it is simple to understand the basic concepts of variables, atoms, and tuples in Erlang.


 




"Erlang Programming" chapter III Basic Concept Learning

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.