erlang 入门(1)

erl
- 命令以.结尾
- %为注释
- 变量必须大写开头 X = 12.
- [1,3.4,true] is a list and {person, 25, "Jason"} is a tuple.
- tuple赋值orpattern match
{ip, IP} = {ip, "192.168.0.1"}.
{Atom, IP} = {ip, "192.168.0.1"}.  
-variable 和atom ,前者大写开头,后者小写开头
- loops are done through recursion
如下例
-module(factorial).
-export([factorial/1]).

factorial(0) -> 1.
factorial(N) ->
    N * factorial(N-1).
- 加减乘除都为实数支持大整数,求余为rem  div    
- 变量名被赋值后,不允许被再次定义
9> Two = 2.  
2  
10> two = 3.  
** exception error: no match of right hand side value 3  
11> Two = 3.  
** exception error: no match of right hand side value 3

-清除变量
  f(变量名) -- 清除对该变量的赋值
  f().      -- 清除所有变量的赋值
 
- .and 和 or 都会计算两边的值 ,若是你期望只做一个短路运算的话 ,那么就需要用到 andalso 好 orelse .这个就是类似于 java的 && || 运算符!
 
- 1.=:= 等同于其他语言中的 == 。  =/=和/= 等同于其他语言中的 !=  这个地方还有疑问!
2.从上面的解析看, =:=比较的类似于java中的内存地址比较 == 比较的是两个地址的值是否相同!
3.其他的还有就是 >= =<

    56> 5 == 5.0.  
    true  
    57> 5 =:= 5.  
    true  
    58> 1=/=0.  
    true  
    59> 1=:=0.  
    false  
    60> 5=:=5.0.  
    false  
    61> 5==5.0.  
    true  
    62> 1/=3.  
    true  

    66> 1 >= 1.  
    true  
    67> 1 =< 1.  
    true  
    68> 1 > 3.  
    false  
    69> 1 < 3.  
    true  

    77> 5 =:= false.  
    false  
    78> 5 ==false.  
    false  

    79> 1 == false.  
    false  
    80> 1 < false.  
    true  

在erlang中没有 true和false的布尔类型变量。 这里的 true 和 false 都是 atom.原子单一的变量(可能描述的不准确)。
在erlang中不同类型之间的比较关系如下所示:
Java代码  收藏代码

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

—— _可以匹配任何变量
6> {_,_,A}={12,23,222}.
{12,23,222}
7> A.
222
8> _.
* 1: variable '_' is unbound
   

posted on 2012-01-18 23:07 cutepig 阅读(27) 评论(1) 编辑 收藏

评论

#1楼[楼主]  回复 引用 查看   

- 列表的匹配
15> [A|B]=[1,2,3,4].
[1,2,3,4]
16> A.
1
17> B.
[2,3,4]

18> f().
ok

19> [A,B|C]=[1,2,3,4].
[1,2,3,4]
20> A.
1
21> B.
2
22> C.
[3,4]
23>

-模块

cutepig@ubuntu:~$ cat geometry.erl
-module(geometry).
-export([area/1]).
area({rectangle,Width,Hit}) -> Width*Hit;
area({circle,R}) -> 3.14159*R*R.

cutepig@ubuntu:~$ erl
Erlang R13B01 (erts-5.7.2) [source] [rq:1] [async-threads:0] [kernel-poll:false]

Eshell V5.7.2 (abort with ^G)
1> c(geometry).
{ok,geometry}
2> geometry:area({rectangle,10,5}).
50
3> geometry:area({circle,1.4}).
6.157516399999999

- 另一个例子
cutepig@ubuntu:~$ cat shop.erl
-module(shop).
-export([cost/1]).

cost(oranges) -> 5;
cost(newspaper) -> 8;
cost(apples) -> 6.


cutepig@ubuntu:~$ cat shop1.erl
-module(shop1).
-export([total/1]).

total([{What,N}|T]) -> shop:cost(What)*N+total(T);
total([]) -> 0.

cutepig@ubuntu:~$ erl
Erlang R13B01 (erts-5.7.2) [source] [rq:1] [async-threads:0] [kernel-poll:false]

Eshell V5.7.2 (abort with ^G)
1> c(shop).
{ok,shop}
2> c(shop1).
{ok,shop1}
3> shop:cost(oranges).
5
4> shop:cost(orangesX).
** exception error: no function clause matching shop:cost(orangesX)
5> shop1:total([{oranges,2},{newspaper,3}]).
34
6>

- 匿名函数
1> Z=fun(X) -> 2*X end.
#Fun<erl_eval.6.13229925>
2> Z(2).
4
3> Double=Z.
#Fun<erl_eval.6.13229925>
4> Double(3).
6
5> Hypot=fun(X,Y) -> math:sqrt(X*X+Y*Y) end.
#Fun<erl_eval.12.113037538>
6> Hypot(3,4).
5.0
7> Hypot(3).
** exception error: interpreted function with arity 2 called with one argument


8> lists:map(Double,[1,2,3,4]).
[2,4,6,8]
9> Even=fun(X) -> (X rem 2)==0 end.
#Fun<erl_eval.6.13229925>
10> Even(2).
true
11> Even(7).
false
12> lists:map(Even,[1,2,3,4,5,6,7,8]).
[false,true,false,true,false,true,false,true]
13> lists:filter(Even,[1,2,3,4,5,6,7,8]).
[2,4,6,8]

多次定义的函数
14> TempConv=fun({c,C}) -> {f, 32+C*9/5};
14> ({f,F}) -> {c, (F-32)*5/9}
14> end.
#Fun<erl_eval.6.13229925>
15> TempConv({c,100}).
{f,212.0}
16> TempConv({f,212}).
{c,100.0}

高阶函数
17> MakeTest=fun(L) -> (fun(X) -> lists:member(X,L) end) end.
#Fun<erl_eval.6.13229925>
18> IsFruit=MakeTest([apple,pear,orange]).
#Fun<erl_eval.6.13229925>
19> IsFruit(apple).
true
20> IsFruit(appleX).
false

自己的for

cutepig@ubuntu:~$ cat lib_misc.
cat: lib_misc.: No such file or directory
cutepig@ubuntu:~$ cat lib_misc.erl
-module(lib_misc).
-export([for/3]).

for(Max,Max,F) -> [F(Max)];
for(I,Max,F) -> [F(I) | for(I+1,Max,F)].


1> c(lib_misc).
{ok,lib_misc}
2> lib_misc:for(1,10,fun(I) -> I end).
[1,2,3,4,5,6,7,8,9,10]
3> lib_misc:for(1,10,fun(I) -> I*I end).
[1,4,9,16,25,36,49,64,81,100]

自己的map和sum

cutepig@ubuntu:~$ cat mylists.erl
-module(mylists).
-export([sum/1]).
-export([map/2]).

sum([H|T]) -> H + sum(T);
sum([]) -> 0.


map(_,[]) -> [];
map(F,[H|T]) -> [F(H) | map(F,T)].

1> c(mylists).
{ok,mylists}
2> mylists:map(fun(X)->2*X end, [1,2,3]).
[2,4,6]
3>

列表解析
1> L=[1,2,3,4].
[1,2,3,4]
2> [2*X || X <- L].
[2,4,6,8]


cutepig@ubuntu:~$ cat mylists.erl
-module(mylists).
-export([sum/1]).
-export([map/2]).
-export([map2/2]).

sum([H|T]) -> H + sum(T);
sum([]) -> 0.


map(_,[]) -> [];
map(F,[H|T]) -> [F(H) | map(F,T)].

map2(F,L) -> [F(X) || X <- L].

1> c(mylists).
{ok,mylists}
2> mylists:map(fun(X) -> X*2 end, [1,2,3]).
[2,4,6]

快速排序

cutepig@ubuntu:~$ cat lib_misc.erl
-module(lib_misc).
-export([for/3]).
-export([qsort/1]).

for(Max,Max,F) -> [F(Max)];
for(I,Max,F) -> [F(I) | for(I+1,Max,F)].

qsort([]) -> [];
qsort([Pivot|T]) -> qsort([X || X <- T, X < Pivot]) ++ [Pivot] ++ qsort([X || X <- T, X >= Pivot]).


1> c(lib_misc).
{ok,lib_misc}
2> lib_misc:qsort([3,2,4,1]).
[1,2,3,4]
2012-01-19 00:26 | cutepig      

导航

<2012年1月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

公告

昵称:cutepig
园龄:4年8个月
粉丝:11
关注:0

搜索

 
 

常用链接

随笔分类(58)

随笔档案(536)

文章档案(1)

我的链接

积分与排名

  • 积分 - 193028
  • 排名 - 443

最新评论

阅读排行榜