`

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

 
阅读更多

一:if 表达式之true的使用

true:在if结构中表示除此之外的所有情况!

对于 if else 的结构 我们采用的是

fun(X) ->
  do some thing ;
fun(_) ->
  do other thing.

 

的方式。
如果采用 if 的方式是怎么实现呢,请看下面

fun() ->
  if  1 =:= 1 ->                   //情况一
    works
  end,
  if  1 =:= 1 ; 1 =:= 2 ->         //情况二
    works
  end ,
  if 1 =:= 1 , 1 =:= 2 ->          //非情况一和二
    fails
  end .
 

 
我们在编译的时候会发现编译报错!这个是为什么呢!
./what_the_if.erl:10: Warning: no clause will ever match
./what_the_if.erl:10: Warning: the guard for this clause evaluates to 'false'

因为 if 1 =:= 1 , 1 =:= 2  <===> if true && false 这个分支永远匹配到并返回一个结果 false .
对于这种情况,其他语言采用的是exception 加 try catch 的方式来处理。但是在erlang中采用的关键字 true来处理。看下面的例子

oh_god(X) ->
    if X =:= 2 -> might_successed ;
        true -> always_here
       end.

-----------------运行结果-------------------------
2> what_the_if:oh_god(2).
might_successed
3> what_the_if:oh_god(3).
always_here
 


对于if 再看个复杂点的:

complex_if(Animal) ->
     Talk = if Animal == cat -> "haha";
               Animal == dog -> "heihei";
               Animal == pig -> "hoho";
                  true -> "hehe"
             end,
             {Animal,"says:" ++ Talk ++ "!"}.
           
运行结果

--------------------------------------
3> what_the_if:complex_if(jhj).
jhj says:hehe !

 

注意分号就是orelse的意思,erlang 的语法有时候看起来很没规律!很强调表示式的匹配。
现在我们回过头来看,对于以下两种方式的使用都是可以的,效率上也没什么区别!

      方式一             方式二              
    if X > Y -> a()        if X > Y  -> a()
     ; true  -> b()         ; X =< Y -> b()
    end。                     end。

    if X > Y -> a()        if X > Y -> a()
     ; X < Y -> b()         ; X < Y -> b()
     ; true  -> c()         ; X ==Y -> c()
    end。                    end。

 

    但是更推荐使用第二种方式,因为这个更易于理解,且语法清晰简单!

回归点:完整的if true的语法结构;

func() ->
   if condition1 -> work1;
      condition2 -> work2;
      true -> work3      //这个地方没有分号
   end.

 

  
要点:在erlnag中,任何一个表达式都是由返回值的。没有java中定义的 void 函数这个类型。

二:另一种条件判断 case
基本语法:
case param of                      //对于什么参数进行判断
    Pattern Guards -> ...          //具体的匹配模式
end.

看个例子:

-module(temperature).
-export([temperature/1]).

temperature(X) ->
    case X of
        {c,N} when N >= 20, N =< 50 -> 'fav';
        {k,N} when N >= 293,N =< 318 -> 'sc fav';
        {f,N} when N >= 68,N =< 113 -> 'fav in us';
        _  -> 'fav in ceus'
    end.
   
-------------等同于-------------

temperature({c,N}) when N >= 20, N =< 50
      -> 'fav';
temperature({k,N}) when N >= 293,N =< 318
      -> 'sc fav';
temperature({f,N}) when N >= 68,N =< 113
      -> 'fav in us';
temperature(_) -> 'fav in ceus'.
end.

看下运行结果:
14> temperature:temperature({c,20}).
fav
15> temperature:temperature({c,2000}).
'fav in ceus'

 


通过以上的介绍,我们知道可以有四种方式来处理if else 这种逻辑结构。

1. fun(Y) -> xx;
   fun(X) -> yy;
   fun(_) -> zz.
  
2. fun(X) ->
     if X == 1 -> xx;
        X == 2 -> yy;
        true -> zz
     end.
    
3. func(X) ->
     if X == 1 -> xx;
        X == 2 -> yy;
        X != 1 , X != 2 -> zz
     end.

4. func(X) ->
     case X of
       X == 1 -> xx;
       X == 2 -> yy;
       _ -> zz
     end.
 



对于以上四种方式改如何选择呢?
1.从执行效率上看,四种方式没什么大的区别
2.第一种方式最扩张性最好,可以对于多个传递的参数进行处理。比如 f(A,B,C) ,如果对于用 case的话,只能传递数组的方式了 f({A,B,C})
3.单纯的if,从理解上来说最容易!
4.具体方式的选用还是看个人喜欢,更容易接受哪种!无强制要求

总之遵循一个原则:
Clean code is when you look at a routine and it's pretty much what you expected

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics