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个
相关推荐
erlang 程序设计 源码 erlang 程序设计 源码 erlang 程序设计 源码erlang 程序设计 源码
erlang程序设计 erlang入门手册
Erlang程序设计(第2版).pdf 高清带目录。 .
书中兼顾了顺序编程、并发编程和分布式编程,重点介绍如何编写并发和分布式的Erlang程序以及如何在多核CPU上自动加速程序,并深入地讨论了开发Erlang应用中至关重要的文件和网络编程、OTP、ETS和DETS等主题。...
erlang程序设计第二版习题答案 ,是我自己写的习题解答,也对照过网上的一些答案,相对来说是更加简洁和符合题意的解答,不过后面几章因为时间问题没有写完,有问题或者意见可以私信找我。
Erlang程序设计 第2版 Erlang程序设计 第2版Erlang程序设计 第2版
[Erlang程序设计]源代码,包括了erlang程序设计一书全部实例代码;
Erlang程序设计中文版 完整书签 erlang programming
erlang编程指南学习笔记,包含具体操作命令例子
Erlang程序设计,包含完整目录和全套源码
NULL 博文链接:https://jias.iteye.com/blog/615679
Erlang程序设计入门经典,中文高清版,随书光盘源码
Erlang并发编程,Erlang程序设计,Erlang中文手册。 学习erlang的好资料。 Erlang是一个结构化,动态类型编程语言,内建并行计算支持。最初是由爱立信专门为通信应用设计的,比如控制交换机或者变换协议等,因此...
erlang程序设计 的相关代码例子程序,比较多了,希望对有需要的童鞋有帮助
erlang发明者写的书。erlang/otp一种高可靠性的平台。
erlang 程序设计(第二版) 中文 pdf
Joe Armstrong [Erlang程序设计].中文版 一本不错的Erlang学习书籍~!
Joe Armstrong [Erlang程序设计].中文版 和随书源代码 一本不错的Erlang学习书籍!