erlang 符号相关基本语法

ErLang语法约定:

  • 大写字母开头的名字(比如Address),表示一个变量,包括参数、局部变量等;
  • 小写字母开头的单词(比如ok),表示一个常量,叫做atom(原子的意思),包括常量名、函数名、模块名等。
  • ErLang的注释用%开头。ErLang用下划线“_”表示任意变量,类似于Java的switch语法里面的default选项。
  • 参数中没使用到的变量用下杠和首字母大写,如_Sample

比较符

opDescription
==equal to
/=not equal to
=<less than or equal to
<less than
>=greater than or equal to
>greater than
=:=exactly equal to
=/=exactly not equal to

1> 1==1.0.
true
2> 1=:=1.0.
false

运算符

 
 
opDescriptionArgument type
+unary +number
-unary -number
+ number
- number
* number
/floating point divisionnumber
bnotunary bitwise notinteger
divinteger divisioninteger
reminteger remainder of X/Yinteger
bandbitwise andinteger
borbitwise orinteger
bxorarithmetic bitwise xorinteger
bslarithmetic bitshift leftinteger
bsrbitshift rightinteger
7> 2#10 band 2#01.
0
8> 2#10 bor 2#01.
3


Boolean表达式

opDescription
notunary logical not
andlogical and
orlogical or
xorlogical xor

短路表达式

Expr1 orelse Expr2
Expr1 andalso Expr2

case A >= -1.0 andalso math:sqrt(A+1) > B of
列表表达式

++
--

1> [1,2,3]++[4,5].
[1,2,3,4,5]
2> [1,2,3,2,1,2]--[2,1,2].
[3,1,2]

二进制表达式

<<>>
<<E1,...,En>>
Ei = Value |
     Value:Size |
     Value/TypeSpecifierList |
     Value:Size/TypeSpecifierList

TypeSpecifierList
  • Type= integer | float | binary | bytes |bitstring | bits | utf8 | utf16 | utf32
  • Signedness= signed | unsigned
  • Endianness= big | little | native
    Unit= unit:IntegerLiteral
11> <<G,H/bitstring>> = <<1,17,42:12>>.
<<1,17,1,10:4>>
12> H.
<<17,1,10:4>>
13> <<1024/utf8>>.
<<208,128>>
赋值时注意增加空格 B=<<1>> 会变成语法错误,因为跟比较符号=<冲突 、、
<<"--\r\n", Data/binary>>,

strchr(Bin, C, I) ->
    case Bin of
        <<_X:I/binary>> ->
            0;
        <<_X:I/binary, C, _Rest/binary>> ->
            I+1;
        _ ->
            strchr(Bin, C, I+1)
    end.

parse_boolean(Value) ->
    case Value of
        true -> true;
        false -> false;
        <<"true">> -> true;
        <<"false">> -> false;
        <<"1">> -> true;
        <<"0">> -> false;
        1 -> true;
        0 -> false
    end.

<<N1:32/integer, N2:32/float>> = Bin.
<< PointID:32/unsigned-little-integer, Value:32/little-float>> = Bin.
case Packet of
    <<Len:16,  PayLoad:Len/binary, 16#ef>> ->
        {body, PayLoad};
    _ ->
        {error, invalid_packet}
end.
    case Info of    
        <<"get ",Str1/bitstring>>->
            io:format("case0:~s~n", [binary_to_list(Str1)]);
        <<1:1,Str1/bitstring>>->
            io:format("case1:~s~n", [Str1])
    end.




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值