Module-to-pattern matching in Erlang

Source: Internet
Author: User

The module is the Basic code unit of Erlang, and the Erl file is compiled with. Beam as the extension, with the UTF8 character set, and the. erl file as shown below:

-module (module name, the same as the file name of the storage module)

-export ([number of method name/input parameter])

Method1 ({a,b,c})->a*b*c;

MEHTOD2 ({d,e})->d-e.

There are two types of module properties: predefined and user-defined.

The data type used in Erlang to represent a function is called fun, which is equivalent to a lambda in Python, and is typically used to

1) Perform the same action on each element of the list

2) Create your own controls

3) Implementation of Reentrant parsing code, parsing the combo or lazy evaluator

Pattern matching is the foundation of Erlang, and case and if expressions make Erlang small and consistent.

Case Expression of

pattern1[when Guard1], expr-seq1;

Pattern2[when guard2]-> expr-seq2;

...

End

If

Guard1-> expr_seq1;

Guard2-> EXPR_SEQ2;

...

End

Erlang has two methods to catch exception errors, one is to enclose the calling function that throws the exception in a Try_catch expression, provides a summary of the information, and the other is to encapsulate the call in a catch expression, providing detailed stack trace information. After catching an exception, you can adjust Erlang:get_stacktrace () to find the most recent stack information.

The introduction of binary, bit-string, and bit-level pattern matching into Erlang is intended to simplify network programming. A binary type is a column of integers or strings placed between double and double greater than sign. Example: 1> Mybin1 = << "Ilove You" >>

Term_to_bingary (term)->bin converted to binary type

Binary_to_term (Bin)->term binary type conversion to Erlang data type

Carefully select the name of the macro and the Erlang code layout to minimize the semantic gap between C and Erlang. In Erlang, the smallest addressing unit is 1 bits, and the bit sequence in the bit string can be accessed directly.

Module-to-pattern matching in Erlang

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.