`

erLang学些笔记4--条件判断语法

 
阅读更多

1.函数的语法:
基本上所有的编程语言都有

function greet (Gender,Name)
   if (condition1 )
     do something
   else if   (condition2)
     do other thing
   else
     do default behave
end
 

 

在erlang中是如何实现呢。
看下面的代码

-module(iff).
-export([greet/2]).

greet(male,Name) ->
  io:format('hello ,Mr ~s!',[Name]);
greet(female,Name) ->
  io:format('hello,Mrs ~s!',[Name]);
greet(_,Name)->
  io:format('hello,~s!',[Name]).

 

看下运行的结果:

1> iff:greet(male,zhaoming.xuezm).
hello ,Mr zhaoming.xuezm!ok
2> iff:greet(female,qiqi).
hello,Mrs qiqi!ok
3> iff:greet(xx,kiti).
hello,kiti!ok
 



其他语言中的if else 表达式

function(Args)
   if X then
      Expression
   else if Y then
      Expression
   else
      Expression
 


在erlang中的表达方式是:

function(X) ->
  Expression;
function(Y) ->
  Expression;
function(_) ->
  Expression.
  
 



注意点:
1.在erlang中对于这种条件的组转采用的 定义同样的函数名,但是通过分号(;)来区分不同的条件分支表达式。  分号(;)代表的是或(||) 逗号(,)代表是并且(&&)
2。~s :~代表的是占位符,s代表替换的是字符创。 其他还有的就是
   ~n <===> \n 换行符
   ~f <===>  浮点型数据
   ~p <===> 可以代替各种类型

  io:format('hello Mr ~s!',[Name]),
 io:format('the num is ~f',[2.3]);
----得到的结果是
5> iff2:greet(male,ming).
hello Mr ming!the num is 2.300000ok


 io:format('hello Mr ~s~n!',[Name]),
 io:format('the num is ~f',[2.3]);
----多了一个~n

3> iff2:greet(male,ming).
hello Mr ming!
the num is 2.300000ok
 



2.| 符号在list中的作用及变量的匹配

例子一:

-module(functions).
-compile(export_all).

head([H|_]) -> H.

second([_,H|_]) ->H.

third([_,_,H|_]) ->H.

------华丽的分割线-------
7> functions:head([1,2,3,4,5]).
1
8> functions:second([1,2,3,4,5]).
2
9> c(functions).                
{ok,functions}
10> functions:third([1,2,3,4,5]).
3
 


3.绑定变量和未绑定变量
先看个例子:

-module(same).
-compile(export_all).

same(X,X) ->
 true;
same(_,_) ->
 false.
 ------华丽的分割线-------
17> same:same(1,2).
false
18> same:same(1,1).                  //为什么输入1和1的时候是true呢
true
 


在erlang中,变量只能被赋值一次,赋值后不允许再次赋值,除非再赋值的值和原来相同!不可变。所以当调用 same(1,1)的时候,先将1赋值给了X,第二个参数也是X,再对X进行赋值的时候,就会进行匹配,若是相同的话,返回true,反之则返回false。

同理,不可以仅仅是对普通变量,其他的也是可以的!看下面的例子

1 -module(datev).
  2 -compile(export_all).
  3
  4 date_validate({Date={Y,M,D},Time={H,Min,S}}) ->                         //这里定义传入的是一个tuple
  5         io:format('the date is (~p) ~p/~p/~p,~n',[Date,Y,M,D]),
  6         io:format('the time is (~p) ~p:~p:~p,~n',[Time,H,Min,S]);
  7 date_validate(_) ->
  8   io:format('invalid date and time ~n').
 
  运行:
  25> datev:date_validate({{2011,8,23},{14,25,00}}).
the date is ({2011,8,23}) 2011/8/23,
the time is ({14,25,0}) 14:25:0,
ok
32> datev:date_validate({{2011,8,23}}).          
invalid date and time
ok
 


这里传入的是一个tuple,若是非tuple的话 则返回是另一个分支 invalid date and time

我们再看一个例子:

-module(tuple2).
-compile(export_all).


tt({XX,YY}) ->                //要求传入的是一个数组,数组中包含两个参数,若是格式不符合,则进入另一个分支
    io:format('this is a test ~p ~p',[XX,YY]);
tt(_) ->
    io:format('no result').
   
-------- 结果的分割线 --------
26> c(tuple2).
{ok,tuple2}
27> tuple2:tt({22,33}).
this is a test 22 33ok
28> tuple2:tt({22}).  
no resultok
29> tuple2:tt(33). 
no resultok
30> tuple2:tt(33,44).
** exception error: undefined function tuple2:tt/2
31> tuple2:tt([33]).
no resultok

 

通过以上两个例子,我们可以看到。对于传入的参数没有任何的限制,可以是任何的类型。不能做到精确的匹配!为了能做到精确的匹配,那么就需要下面这个角色出现!护卫。


4.guards
guards:为额外的子句,使得函数更具有表述性!
比如说需要定义一个函数,使得16岁以上的人才允许饮酒。按照我们之前学到的语法,那么需要这么定义!

old_enough(0) --> false;
old_enough(0) --> false;
old_enough(0) --> false;
....
old_enough(15) --> false;
old_enough(X) --> true;

这个时候就需要我们的guards出现了!
old_enough(X)when X >=16 -> true;
old_engouh(_) false.

-------- 解决方案的分割线 ----------
-module(old_enough).
-compile(export_all).

old_enough(X) when X >=16 ->
    true;
old_enough(_) ->
    false.
 


   
再看几个例子
判断一个人的年龄是否在一个正确的范围内!

-module(right_age).
-compile(export_all).

right_age(X) when X >= 18 , X < 65 ->      //注意这里用的是逗号,在这个表达式中逗号表示 &&
    true;
right_age(_) ->
    false
 



运行结果:

    37> c(right_age).
{ok,right_age}
38> right_age:right_age(33).
true
39> right_age:right_age(333).
false
 


判断年龄的另一种方式

-module(wrong_age).
-compile(export_all).

wrong_age(X) when X < 18 ; X> 60  ->   //这里用的是分号,在这个表达式分号,分号代表的是 || 或
    true;
wrong_age(_) ->
    false.
 



结果:

40> c(wrong_age).
{ok,wrong_age}
41> wrong_age:wrong_age(3).
true
42> wrong_age:wrong_age(33).
false
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics