erlang-基本语法
基础语法
% hello world program
-module(helloworld). 
-export([start/0]). 
start() -> 
   io:fwrite("Hello, world!\n").
%注释
module模块声明,代码是helloworld的一部分
/0 则表 start 函数接受 0 个参数
export使函数可以在其他地方使用
io模块提供输入输出,fwrite输出到控制台
#模块
-module(ModuleName)
#import
#Modulename − 这是需要被导入的模块的名称。
#functionname/parameter − 这是要被导入的在模块中的函数
-import (modulename, [functionname/parameter]).
使用import
% hello world program
-module(helloworld).
-import(io,[fwrite/1]).
-export([start/0]).
start() ->
   fwrite("Hello, world!\n").
关键词
after	and	    andalso	band
begin	bnot	bor	    bsl
bsr	    bxor	case	catch
cond	div	    end	    fun
if	    let	    not	    of
or	    orelse	receive	rem
try	    when	xor	
Erlang shell
#打印当前绑定变量
b(). 
#f()删除所有当前变量绑定
f().
#移除指定变量的绑定
f(x).
#打印在 shell 中执行过所有命令的历史记录列表
h().
#设置之前命令的数量,以保持在为 N 个历史记录。
history(N).
#如果N为正重复此命令N次。如果N是负数,则第N个先前的命令被重复
e(N).
数据类型
数字:两个数字相加
-module(helloworld).
-export([start/0]).
start() ->
   io:fwrite("~w",[1+1]).
原子类型
原子必须以小写字母开头,并且可以包含大写和小写字母,数字,下划线(_)和“at”符号(@)。我们还可以在单引号中使用原子。
创建一个atom1原子
-module(helloworld).
-export([start/0]).
start() ->
   io:fwrite(atom1).
布尔值
-module(helloworld).
-export([start/0]).
start() ->
   io:fwrite(2 =< 3).
位字符串
-module(helloworld).
-export([start/0]).
start() ->
   Bin1 = <<10,20>>,
   X = binary_to_list(Bin1),
   io:fwrite("~w",[X]).
元组
定义一个元组 P,并有3项数据
tuple_size是在 Erlang 中定义的一个内置函数,它是用来确定一个元组的大小。
-module(helloworld). 
-export([start/0]). 
start() -> 
   P = {john,24,{june,25}} , 
   io:fwrite("~w",[tuple_size(P)]).
映射
-module(helloworld). 
-export([start/0]). 
start() -> 
   M1 = #{name=>john,age=>25}, 
   io:fwrite("~w",[map_size(M1)]).
列表
-module(helloworld). 
-export([start/0]). 
start() -> 
   L = [10,20,30] , 
   io:fwrite("~w",[length(L)]).
变量
变量和不可变的,如果需要修改值,需要销毁后重新创建;
例子
数字 − 此用于表示整数或浮点数。一个例子是:10 
布尔 − 这表示的一个布尔值可以是 true 或 false ;
位字符串 − 位序列(字符串)用来存储非类型化的内存区域。一个例子是:<<40,50>>.
元组 − 元组是具有固定数量混合数据类型的术语。一个例子是: {40,50}.
映射 − 映射是用 键-值关联的可变数量的复合数据类型。映射中的每个键值关联称为关联对。一个例子是 {type=>person,age=>25}.
列表 − 列表是可变数量的混合数据类型的一个术语。一个例子是 [40,40].
变量声明
-module(helloworld). 
-export([start/0]). 
start() -> 
   X = 40, 
   Y = 50, 
   Result = X + Y, 
   io:fwrite("~w",[Result]).
再次强调:变量需要大写开头,只能赋值一次
打印变量
-module(helloworld). 
-export([start/0]). 
start() -> 
   X = 40.00, 
   Y = 50.00, 
   io:fwrite("~f~n",[X]), 
   io:fwrite("~e",[Y]).
参数
~ − 此字符标志着需要执行某些格式输出;
~f −参数是 float 被写为 [-]ddd.ddd; 
~n − 类似于 println 输出一个新行; 
~e − 参数是 float 被写为 [-]d.ddde+-ddd ; 
运算符
算术运行符
操作符	    描述	                                示例
+	        两个操作数相加                      	1 + 2 = 3
−	        从第一个操作数减去第二个操作数	        1 - 2 = -1
*	        两个操作数相乘	                        2 * 2 = 4
/	        通过分子除以分母                       2 / 2 = 1
rem	        第二数除以第一数的余数                  3 rem 2 = 1
div	        div组件将执行除法并返回整数部分          3 div 2 = 1
例子
-module(helloworld). 
-export([start/0]). 
start() -> 
   X = 40, 
   Y = 50, 
   
   Res1 = X + Y, 
   Res2 = X - Y, 
   Res3 = X * Y, 
   Res4 = X / Y, 
   Res5 = X div Y, 
   Res6 = X rem Y, 
   
   io:fwrite("~w~n",[Res1]), 
   io:fwrite("~w~n",[Res2]), 
   io:fwrite("~w~n",[Res3]), 
   io:fwrite("~w~n",[Res4]), 
   io:fwrite("~w~n",[Res5]), 
   io:fwrite("~w~n",[Res6]).
关系运算符
操作符   描述	                            示例
==	    测试两个对象是否相等	            2 = 2 结果为 true
/=	    测试两个对象是否为不相等            	3 /= 2 结果为 true
<	    测试左边对象是否小于右边操作对象	    2 < 3 结果为 true
=<	    测试左边对象是否小于或等于右边操作对象	2 =<3 结果为 true
>	    测试左边对象是否大于右边操作对象    	3 > 2 结果为 true
>=	    测试左边对象是否大于或等于右边操作对象	3 >= 2 结果为 true
例子
-module(helloworld).-export([start/0]). 
start()-> 
   io:fwrite("~w~n",[3==2]), 
   io:fwrite("~w~n",[3/=2]), 
   io:fwrite("~w~n",[3<2]), 
   io:fwrite("~w~n",[3=<2]), 
   io:fwrite("~w~n",[3>2]), 
   io:fwrite("~w~n",[3>=2]).
逻辑运算符
操作符	描述	                示例
or	    逻辑 “或”操作符	    true 或 true 结果为 true
and 	逻辑 “与”操作符	    True 与 false 结果为 false
not	    逻辑 “非”操作符	    非 false 结果为 true
xor	    逻辑 “异或”操作符	    True异或 false 结果为 false
例子
-module(helloworld).-export([start/0]). 
start()-> 
   io:fwrite("~w~n",[trueorfalse]),  
   io:fwrite("~w~n",[trueandfalse]), 
   io:fwrite("~w~n",[true xor false]), 
   io:fwrite("~w~n",[notfalse]).
位运算符
band      
bor        
bxor
bnot
例子
-module(helloworld). 
-export([start/0]). 
start() -> 
   io:fwrite("~w~n",[00111100 band 00001101]), 
   io:fwrite("~w~n",[00111100 bxor 00111100]), 
   io:fwrite("~w~n",[bnot 00111100]), 
   io:fwrite("~w~n",[00111100 bor 00111100]).
优先级
:	
#	
bnot,not	
/,*,div,rem,band,and	左关联
+,-,bor,bxor,or,xor 	左关联
==,/=,=<,<,>=,>
循环while和for
while语句
while只能递归实现,因为没有直接的while语句

- 定义一个名为 while 递归函数执行 while 循环;
- 输入列表的值在定义变量X到 while 功能作为一个实例;
- while 功能同时会取到每个列表中的值并存储中间值到变量“‘Acc’”;
- while循环递归调用列表中的每个值;
-module(helloworld). 
-export([while/1,while/2, start/0]). 
while(L) -> while(L,0). 
while([], Acc) -> Acc;
while([_|T], Acc) ->
   io:fwrite("~w~n",[Acc]), 
   while(T,Acc+1). 
   
   start() -> 
   X = [1,2,3,4], 
   while(X).
for语句
- 我们定义一个递归函数来实例和执行 for 循环;
- 我们使用的是 ‘for’ 函数以确保 N 或限制的值是正值;
- 我们递归地调用 for 函数,通过在每一次递归后减少N的值。
-module(helloworld). 
-export([for/2,start/0]). 
for(0,_) -> 
   []; 
   for(N,Term) when N > 0 -> 
   io:fwrite("Hello~n"), 
   [Term|for(N-1,Term)]. 
   
start() -> 
   for(5,1).
决策
if
如果条件为真,则 statement#1 将被执行,否则 statement#2 将被执行。
if
condition ->
   statement#1;
true ->
   statement #2
end.
例子
-module(helloworld). 
-export([start/0]). 
start() -> 
   A = 5, 
   B = 6, 
   
   if 
      A == B -> 
         io:fwrite("True"); 
      true -> 
         io:fwrite("False") 
   end.
多表达式
if
condition1 ->
   statement#1;
condition2 ->
   statement#2;
conditionN ->
   statement#N;
true ->
   defaultstatement
end.
例子
-module(helloworld). 
-export([start/0]). 
start() -> 
   A = 5, 
   B = 6, 
   if 
      A == B -> 
         io:fwrite("A is equal to B"); 
      A < B -> 
         io:fwrite("A is less than B"); 
      true -> 
         io:fwrite("False") 
   end.
内嵌if
-module(helloworld). 
-export([start/0]). 
start() -> 
   A = 4, 
   B = 6, 
   if 
      A < B ->
      
         if 
            A > 5 -> 
               io:fwrite("A is greater than 5"); 
            true -> 
               io:fwrite("A is less than 5")
         end;
      true -> 
         io:fwrite("A is greater than B") 
   end.
case
case expression of
   value1 -> statement#1;
   value2 -> statement#2;
   valueN -> statement#N
end.
例子
-module(helloworld). 
-export([start/0]). 
start() -> 
   A = 5,
   case A of 
      5 -> io:fwrite("The value of A is 5"); 
      6 -> io:fwrite("The value of A is 6") 
   end.
函数
定义
FunctionName(Pattern1… PatternN) ->
    Body;
Pattern1… PatternN − 每个参数都是一个模式
Body − 一个子句主体是由以逗号分隔的表达式序列组成 (,)
例子
-module(helloworld). 
-export([add/2,start/0]). 
add(X,Y) -> 
   Z = X+Y, 
   io:fwrite("~w~n",[Z]). 
   
start() -> 
   add(5,6).
匿名函数
-module(helloworld). 
-export([start/0]). 
start() -> 
   Fn = fun() -> 
      io:fwrite("Anonymous Function") end, 
   Fn().
例子中fun()定义匿名函数,分配给Fn变量
参数的函数
-module(helloworld). 
-export([add/2,add/3,start/0]). 
add(X,Y) -> 
   Z = X+Y, 
   io:fwrite("~w~n",[Z]). 
   
add(X,Y,Z) -> 
   A = X+Y+Z, 
   io:fwrite("~w~n",[A]). 
 
start() ->
   add(5,6), 
   add(5,6,6).
函数使用序列
表达式只有当评估(计算)为 true 时函数才运行。
FunctionName(Pattern1… PatternN) [when GuardSeq1]->
Body;
GuardSeq1 − 当函数被调用它获取计算的表达式。
-module(helloworld). 
-export([add/1,start/0]). 
add(X) when X>3 -> 
   io:fwrite("~w~n",[X]). 
start() -> 
   add(4).
模块
属性
-module(helloworld). 
-author("TutorialPoint"). 
-version("1.0"). 
-export([start/0]). 
start() -> 
   io:fwrite("Hello World").
导入
-module(helloworld). 
-import(io,[fwrite/1]). 
-export([start/0]). 
start() -> 
   fwrite("Hello, world!\n").
递归
-module(helloworld). 
-export([fac/1,start/0]). 
fac(N) when N == 0 -> 1; 
fac(N) when N > 0 -> N*fac(N-1). 
start() -> 
   X = fac(4), 
   io:fwrite("~w",[X]).
首先定义一个函数:fac(N)
长度递归
-module(helloworld). 
-export([len/1,start/0]). 
len([]) -> 0; 
len([_|T]) -> 1 + len(T). 
start() -> 
   X = [1,2,3,4], 
   Y = len(X), 
   io:fwrite("~w",[Y]).
第一个函数 len([]) 用于特殊情况的条件:如果列表为空。
[H|T] 模式来匹配一个或多个元素的列表,如长度为1的列表将被定义为 [X|[]],而长度为 2 的列表将被定义为 [X|[Y|[]]] 。
尾部递归
-module(helloworld).
-export([tail_len/1,tail_len/2,start/0]). 
tail_len(L) -> tail_len(L,0). 
tail_len([], Acc) -> Acc; 
tail_len([_|T], Acc) -> tail_len(T,Acc+1). 
start() -> 
   X = [1,2,3,4], 
   Y = tail_len(X), 
   io:fwrite("~w",[Y]).
重复
-module(helloworld). 
-export([duplicate/2,start/0]). 
duplicate(0,_) -> 
   []; 
duplicate(N,Term) when N > 0 ->
   io:fwrite("~w,~n",[Term]),
   [Term|duplicate(N-1,Term)]. 
start() -> 
   duplicate(5,1).
列表反转
-module(helloworld). 
-export([tail_reverse/2,start/0]). 
tail_reverse(L) -> tail_reverse(L,[]).
tail_reverse([],Acc) -> Acc; 
tail_reverse([H|T],Acc) -> tail_reverse(T, [H|Acc]).
start() -> 
   X = [1,2,3,4], 
   Y = tail_reverse(X), 
   io:fwrite("~w",[Y]).
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号