Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between binary string and ordinary list

Tags:

erlang

I'm new to erlang, and in erlang shell, i typed below expression:

A= <<"abc">>.
B="abc".

I want to know the differences between A and B, and their general usage, Also why below expression is not correct:

C=<<abc>>.
like image 947
Huang_Hai_Feng Avatar asked May 05 '15 14:05

Huang_Hai_Feng


1 Answers

A= <<"abc">> is a binary. Binary is a data type in Erlang. Bit syntax encloses binary data between << and >>. In this case sequence of bits. So here the binary is 3, 8 bit values of 97,98,99 (decimal) in memory. Erlang is very powerful in handling binary data. As this is built-in, it is very efficient and there are many functions to handle binary operations.

B="abc" is th string representation. There is no seperate data type string in Erlang. Strings in Erlang are simply lists of characters with a bit of syntactic sugar (lists as text enclosed within quotation marks). So it is same as B=[$a,$b,$c]. Hence it is a list of integers (atleast 32 bits + pointer to next) intead of 8/16 bits per characters in other languages. Though it can handle unicode it is less effecient for large strings.

abc is an atom and that cannot be inside binary (unless converted). So C=<<abc>>. is not correct.

like image 104
Vinod Avatar answered Oct 16 '22 08:10

Vinod