erlang operator 备忘

原创文章,转载请注明来源并保留原文链接

erlang中的运算符
1)算术运算符

Arithmetic Operators

Arithmetic Operators

1> +1.
1
2> -1.
-1
3> 1+1.
2
4> 2/2.
1.0
5> 5 div 3.
1
6> 5 rem 3.
2

band是按位与运算,bor是按位或运算,bxor是按位异或,bsl按位左移,bsr按位右移,注意最后的结果都是二进制。

1> 2#10 band 2#110.
2
2> 3#10 bor 2#100.
7 %这里3#10先换算成了2#11
3> 2#10 bxor 2#11.
1
4> 2#10 bsl 2.
8
5> 2#10 bsr 2.
0

2)比较运算符

Comparison Operators

Comparison Operators

data type的大小排序:

number < atom < reference < fun < port < pid < tuple < list < bit string

=:=和==,/=和=/=的区别:

1> 1 == 1.0.
true
2> 1=:=1.0.
false
3> 1 /= 1.0.
false
4> 1 =/= 1.0.
true

3)逻辑运算符
四个,not,and,or,xor

1> not true.
false
2> true and false.
false
3> true xor false.
true
4> true or garbage.
** exception error: bad argument

4)数组运算
最后还有个数组运算,++或者–,很简单

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

Leave a Reply

Your email address will not be published. Required fields are marked *