Erlang -- Addendum

Source: Internet
Author: User

Reprinted: http://hi.baidu.com/let163/blog/item/f4877311703faedbf7039ef6.html

1. The reserved characters of Erlang include:

After and andalso band begin bnot Bor bsl bsr bxor case catch cond Div end fun if let not of or orelse query receive REM try when XOR

These are basically symbols used for logical operations, bit operations, and special expressions.

2. The types of Erlang include:
1) binary, used to indicate an unknown memory area
For example:
1><10, 20>.
<10, 20>
2><"ABC">.
<65,66, 67>

2) reference, by calling the unique term generated by mk_ref/0

3) string, string, and string in Erlang are included in double quotation marks, which are actually list. During compilation, two adjacent strings are connected. For example, "string" "42" is equivalent to "string42"

4) Record: record type. Similar to struct in C, a module can be declared through the-record attribute, for example:
-Module (person ).
-Export ([New/2]).
-Record (person, {name, age }).
New (name, age)->
# Person {name = Name, age = age }.
1>Person: New (Dennis, 44 ).
{Person, Dennis, 44}
After compilation, it is actually converted to tuple. You can use name # person. Name to access the name attribute of Name Record.

3. pre-defined attributes of the module:
-module(Module).Declare the module name, which must be the same as the file name
-export(Functions).List of functions to be exported to the outside world
-import(Module,Functions).The introduced function can be used as a locally defined function.
-compile(Options).Set compilation options, such as export_all
-Vsn (vsn). This option is set for the module version. You can usebeam_lib:version/1Obtain this information
You can use-include and-include_lib to include files. The difference between the two is that include-lib cannot find files through absolute paths, but is searched in your current Erlang lib directory.

4. Try expression. Try expression can be used with catch, for example:

Try expr
Catch
Throw: Term-> term;
Exit: reason-> {'exit ', reason}
Error: reason-> {'exit ', {reason, Erlang: get_stacktrace ()}}
End

Besides, try can also be used in combination with after, similar to try... finally in Java for clearing, for example:
Termize_file (name)->
{OK, f} = file: open (name, [Read, binary]),
Try
{OK, Bin} = file: Read (F, 1024*1024 ),
Binary_to_term (BIN)
After
File: Close (f)
End.


5. List comprehensions (list comprehensions), one of the functional language features, the syntax in Erlang is similar:
[Expr | qualifier1,..., qualifiern]
Expr can be any expression, while qualifier is generator or filter. For example.
1>[X * 2 | x <-[1, 2, 3].
[2, 4, 6]

2> L = [1, 2, 4, 5, 6, 7].
[1, 2, 3, 4, 5, 6, 7]

3> [x | x <-L, x> = 3].
[3, 4, 5, 6, 7]

Let's look at some cool examples, from programming Erlang,
For example, quick sorting:
-Module (qsort ).
-Export ([qsort/1]).
Qsort ([])-> [];
Qsort ([bytes | T])->
Qsort ([x | x <-t, x

6. Macros, defining constants or functions, etc. The syntax is as follows:
-Define (const, replacement ).
-Define (func (var1,..., Varn), replacement ).

Add a question mark before the macro name when using it ?, For example? Const and replacement Insert the position where the macro appears. The system predefines some macros:
? Module indicates the name of the current module.

? Module_string is the same as above, but in the string form
? File Name of the current Module
? Number of lines of current Code called by line
? Machine name

The macros of Erlang are similar to those of C. They also have macro indicators, including:

-undef(Macro).
Cancel macro definition
-ifdef(Macro).
When macro is defined, execute the following code:
-ifndef(Macro).
Same as above, and vice versa
-else.
After ifdef or ifndef is connected, the following code is executed if the former condition is not met.

-endif.
If Terminator

Suppose macro-define (square (x), x * X). used to calculate the square, then ?? X returns the string form of the X expression, similar to # ARG in C

A simple macro example: Ruby code

  1. -Module(Macros_demo ).
  2. -Ifdef (Debug ).
  3. -Define (log (x), IO: Format ("{~ P ,~ P }:~ P ~ N ",[? Module ,? Line, X]).
  4. -Else.
  5. -Define (log (x ),True).
  6. -Endif.
  7. -Define (square (x), x * X ).
  8. -Compile (export_all ).
  9. Test ()->
  10. A = 3,
  11. ? Log (),
  12. B =? Square (),
  13. IO: Format ("Square (~ W) is ~ W ~ N ", [a, B]).

When the debug option is not enabled during compilation:
17> C (macros_demo ).
{OK, macros_demo}
18> macros_demo: Test ().
Square (3) is 9

After debugging is enabled during compilation:

19> C (macros_demo, {d, debug }).
{OK, macros_demo}
20> macros_demo: Test ().
{Macros_demo, 11 }:3
Square (3) is 9
OK

The log output shows the number of lines, module names, and parameters.

7. Process dictionary. Each process has its own process dictionary used to store global variables in the process.
BIFS operation:
Put (Key, value)
Get (key)
Get ()
Get_keys (value)
Erase (key)
Erase ()

8. Requirements for distributed programming
1) The default connection between nodes is transitive, that is, when node A connects Node B and Node B connects node C, node A also connects to node C.
You can disable the default behavior by specifying the parameter-connect_all false when starting the node.

2) hide a node. In some cases, you want to connect to a node instead of other nodes. You can specify the-hidden option when the node starts.
To start a hidden node. In this case, you can use nodes () to view all connected nodes. hidden nodes are not displayed.
You can view it through nodes (hidden) or nodes (connected.

The complete ERL options are as follows:

-connect_all false As explained above.
-hidden Start a hidden Node
-name Name Start a system to become a node and use long name.
-setcookie Cookie AndErlang:set_cookie(node(), Cookie). Similarly, set Magic cookie
-sname Name Start an Erlang system as a node and use short name

Note that the node started by short name cannot communicate with the Long Name node.

. A small detail, in Erlang, is less than equal to =, rather than the <= syntax in general language. Where I made a mistake, it is also not equal to the use of a/number, rather
!, For example,/=, =/=.

10. Differences between and or and andalso orelse

And and or will calculate the expressions on both sides, while andalso and orelse adopt short-circuit mechanism, such as exp1 andalso exp2. When exp1 returns false, it will not be evaluated.
Exp2 directly returns false, while exp1 and exp2 evaluates both exp1 and exp2, or is similar to orelse.

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.