TUPLE_TO_LIST/1 converts a tuple to a list, time/0 returns the current time, minutes, seconds.
1> tuple_to_list ({12,cat, "ddd"}).
[12,cat, "ddd"]
3> time ().
{12,35,57}
2. Binary data:
A data type used to achieve high-speed storage of raw data. Save memory and input and output more efficiently. When writing a print, the binary data appears as an integer or sequence of characters, surrounded by angle brackets, respectively. One of the integers, each one to be between 0-255, if the binary data is a printable string, the shell will display a string, otherwise a string of integers will be displayed.
@spec describes the parameters and return types of the function. Type callouts, rather than Erlang code, are part of the comment document and cannot be used in the shell. Module declarations in Erlang are also part of the annotations.
Erlang constructs binary data or extracts data from it through BIF, or through bit syntax to complete the process.
@spec list_tbo_inary (iolist), binary ()
@spec split_binary (Bin,pos), {bin1,bin2}
@spec term_to_ Binary (term), bin
@spec binary_to_term (bin), term
List_tbo_inary converts everything in iolist to a binary data. Split_binary splits binary data into two parts at the POS location. The following two are reciprocal.
4> Bin1 = <<1,2,3>>.
<<1,2,3>>
5> Bin2 = <<4,5>>.
<<4,5>>
6> Bin3 = <<6>>.
<<6>>
7> list_to_binary ([bin1,1,[2,3,bin2],4| Bin3]).
<<1,2,3,1,2,3,4,5,4,6>>
12> split_binary (<<1,2,3,1,2,3,4,5,4,6>>,4).
{<<1,2,3,1>>,<<2,3,4,5,4,6>>
14> term_to_binary ({One, ' 333a ', use}).
<<131,104,3,97,11,100,0,4,51,51,51,97,100,0,3,117,115,101>>
15> binary_to_term (<< 131,104,3,97,11,100,0,4,51,51,51,97,100,0,3,117,115,101>>).
{One, ' 333a ', use}
returns the binary data byte length
16> size (<<1,2,3,4>>).
4
3. Bit syntax
bit syntax: A pattern-matching syntax for bits in binary data for packet-and-packet work. The
bit syntax is an extension of pattern matching. When writing the underlying code, it is often necessary to packet-unpack the bit-level binary data, which will reflect the convenience of bit syntax and the bit syntax designed for protocol programming (Erlang's housekeeping skills-wow plug).
Packet unpacking of 16bit colors
19> Red = 2.
2
20> Green = 54.
54
21> Blue = 20.
20
22> men = <<red:5,green:6,blue:5>>.
<<22,212>>
23> Mem = <<red:5,green:5,blue:5>>.
<<21,84:7>>
24> <<R1:5,G1:6,B1:5>> = men.
<<22,212>>
25> R1.
2
27> G1.
54
28> B1.
20
You can see that you are using: To make a match, before the colon is the data, and then the number of bits that are occupied.
Bit-Syntax expressions
Well, here's the bit syntax format:
The form of bit syntax:<<>> or <<e1,e2,e3,e4,..., en>>. There are four types of EI:
Ei = Value | Value:size | Value/typespecifierlist | Value:size/typespecifierlist
The total number of bits in the binary data is exactly divisible by 8 (each byte in binary data is 8bit). Value must be a bound variable, a text string, or an integer that returns a value. Expressions for floating-point and binary data. Size must be an integer or integer bound variable, not a free variable. The integer default size is 8, the float type is 64, and the binary is its own length. Specifierlist determines the byte order, and the value is:
@type End = big| Little |native
The book gives an example of how these three sorts and the default sort, different machines may be different.
37> {<<16#12345678:32/big>>,<<16#12345678:32/little>>,<<16#12345678:32/native >>,<<16#12345678:32>>}.
{<<18,52,86,120>>,
<<120,86,52,18>>,
<<120,86,52,18>>,
<<18,52,86,120>>}
4. Summary of Use
Block expression:
Begin
EXPR1,
....
Exprn
End
The chunk value is the value of the last expression in the fast, used when a single expression is allowed somewhere in the code and you want to use a string of expressions.
Comments:
Only line Comment%, no block comment.
List operator ++--: infix operator for adding and removing lists.
Comparison expressions:
The size comparison order is defined for all types:
Number<atom<reference<fun<port<pid<tuple<list<binary
Role: You can sort the list that stores any type and write efficient data access code based on the order of comparison.
Outside the =:=,=/=, the others follow the following rules:
If a comparison parameter is an integer, another floating-point number, the integer needs to be converted to a floating-point number before comparison.
If the two comparison parameters are integers or floating-point numbers, direct comparison ...
= = Applies only to the comparison of floating-point numbers and integers. It's best to use =:=.
Underline variable:
If a variable is used only once in a sentence, the compiler warns. But the underscore starts, and the compiler does not produce a warning message.
Name the variable that is not ready for use, increasing readability. Easy to Debug.