See Also http://www.erlang.org/course/sequential_programming.html
Numbers
Integers
Floats
Atoms
Tuples
Lists
Variables
Complex Data Structures
Pattern Matching
Function Calls
The Module System
Built in Functions(BIFs)
Function syntax
An example of function evaluation
Examples of Guards
Traversing Lists
Lists and Accumulators
Shell commands
Special Functions
Special Forms
Numbers数字
Integers 10 -234 16#AB10F 2#110111010 $A Floats 17.368 -56.654 12.34E-10. * B#Val 表示B进制数字 * $Char 获取Char字符的Ascii码
Atoms元子
abcef start_with_a_lower_case_letter 'Blanks can be quoted' 'Anything inside quotes \n\012' *任意长度字符串 *atom里面支持任意字符
Tuples数组-{}
{123, bcd} {123, def, abc} {person, 'Joe', 'Armstrong'} {abc, {def, 123}, jkl} {} *存储固定数量的一组数据 *数组长度可任意指定
Lists可变数组-[]
[123, xyz] [123, def, abc] [{person, 'Joe', 'Armstrong'}, {person, 'Robert', 'Virding'}, {person, 'Mike', 'Williams'} ] "abcdefghi" 表示 - [97,98,99,100,101,102,103,104,105] "" 表示空数组 - [] *存储可变元素 *长度动态调整
Variables变量
Abc A_long_variable_name AnObjectOrientatedVariableName *首字母大写 *用于存储数据 *只可赋值一次,一旦设置后不能改变
Complex Data Structuresz自定义数据结构
[{{person,'Joe', 'Armstrong'}, {telephoneNumber, [3,5,9,7]}, {shoeSize, 42}, {pets, [{cat, tubby},{cat, tiger}]}, {children,[{thomas, 5},{claire,1}]}}, {{person,'Mike','Williams'}, {shoeSize,41}, {likes,[boats, beer]}, ... *可以自定义数据结构 *不需要声明内存申请释放 *可以包含赋值了的变量
Pattern Matching模式匹配
A = 10 Succeeds - binds A to 10 {B, C, D} = {10, foo, bar} Succeeds - binds B to 10, C to foo and D to bar {A, A, B} = {abc, abc, foo} Succeeds - binds A to abc, B to foo {A, A, B} = {abc, def, 123} Fails [A,B,C] = [1,2,3] Succeeds - binds A to 1, B to 2, C to 3 [A,B,C,D] = [1,2,3] Fails Pattern Matching (Cont) [A,B|C] = [1,2,3,4,5,6,7] Succeeds - binds A = 1, B = 2, C = [3,4,5,6,7] [H|T] = [1,2,3,4] Succeeds - binds H = 1, T = [2,3,4] [H|T] = [abc] Succeeds - binds H = abc, T = [] [H|T] = [] Fails {A,_, [B|_],{B}} = {abc,23,[22,x],{22}} Succeeds - binds A = abc, B = 22 * 字符"_"表示匿名变量,不会使用此变量,忽略不关心的值
Function Calls
module:func(Arg1, Arg2, ... Argn) func(Arg1, Arg2, .. Argn) *Arg1 ... Argn是erlang数据结构 *函数名和模块名必须要atom元子 *函数可以不包含输入参数 *函数定义在模块内 *函数在模块外调用时需要被export导出
Module System
-module(demo). -export([double/1]). double(X) -> times(X, 2). times(X, N) -> X * N. *double函数可以在模块外调用,times函数只能在模块内调用 *double/1表示传入一个参数的double函数(double/1和double/2是两个不同函数)
Starting the system
unix> erl Eshell V2.0 1> c(demo). double/1 times/2 module_info/0 compilation_succeeded 2> demo:double(25). 50 3> demo:times(4,3). ** undefined function:demo:times[4,3] ** ** exited: {undef,{demo,times,[4,3]}} ** 4> 10 + 25. 35 5> *c(File) 指令会编译File.erl文件 *1>,2>是shell提示符
Built In Functions(BIFs)
date() time() length([1,2,3,4,5]) size({a,b,c}) atom_to_list(an_atom) list_to_tuple([1,2,3,4]) integer_to_list(2234) tuple_to_list({}) *这些函数内置在erlang自带的模块中
Function Syntax
func(Pattern1, Pattern2, ...) -> ... ; func(Pattern1, Pattern2, ...) -> ... ; ... func(Pattern1, Pattern2, ...) -> ... . *分句是被顺序扫描直至匹配 *匹配后给每个变量赋值 *变量是局部的,自动申请释放
Functions(cont)
-module(mathStuff). -export([area/1]). area({square, Side}) -> Side * Side; area({circle,Radius}) -> 3.14 * Radius * Radius; area({triangle,A,B,C}) -> S = (A+B+C)/2, math:sqrt(S*(S-A)*(S-B)*(S-C)); area(Other) -> {invalid_object,Other}.
Guarded Function Clauses
-module(mathStuff). -export([factorial/1,errcase/1]). factorial(0) -> 1; factorial(N) when N > 0 -> N * factorial(N-1). %upper is not the same as below errcase(0) -> 1; errcase(N) -> N * errcase(N-1).
Examples of Guards
number(X) - X is a number integer(X) - X is an integer float(X) - X is a float atom(X) - X is an atom tuple(X) - X is a tuple list(X) - X is a list length(X) == 3 - X is a list of length 3 size(X) == 2 - X is a tuple of size 2. X > Y + Z - X is > Y + Z X == Y - X is equal to Y X =:= Y - X is exactly equal to Y (i.e. 1 == 1.0 succeeds but 1 =:= 1.0 fails) *guard中的变量必须被赋值
Special Functions
apply(Mod, Func, Args)
传入Func函数,对Args参数进行处理
1> apply( lists1,min_max,[[4,1,7,3,9,10]]).
{1, 10}
Special Forms
case lists:member(a, X) of true -> ... ; false -> ... end, ... if integer(X) -> ... ; tuple(X) -> ... end, ...
Shell Commands
h() - 打印20条历史命令
b() - 查看所有变量赋值情况
f() - 取消所有变量赋值
f(Var) - 取消指定变量的赋值(注:这个功能只能用在shell命令中,不能用在函数体)
e(n) - 执行历史记录中第n个