鍍金池/ 教程/ 區(qū)塊鏈/ if 與 case
注冊進程名稱
錯誤處理
完整示例
分布式編程
消息傳遞
if 與 case
健壯性
映射 (Map)
高階函數(shù) (Fun)
輸出至終端
更多關(guān)于列表的內(nèi)容
內(nèi)置函數(shù) (BIF)
模塊與函數(shù)
將大程序分在多個文件中
匹配、Guards 與變量的作用域
超時
列表
完整示例
頭文件
標準模塊與使用手冊
進程
記錄
增加健壯性后的完整示例
Erlang Shell
原子類型

if 與 case

上面的 find_max_and_min 函數(shù)可以找到溫度的最大值與最小值。這兒介紹一個新的結(jié)構(gòu) if。If 的語法格式如下:

if
    Condition 1 ->
        Action 1;
    Condition 2 ->
        Action 2;
    Condition 3 ->
        Action 3;
    Condition 4 ->
        Action 4
end

注意,在 end 之前沒有 “;”。條件(Condidtion)的工作方式與 guard 一樣,即測試并返回成功或者失敗。Erlang 從第一個條件開始測試一直到找到一個測試為真的分支。隨后,執(zhí)行該條件后的動作,且忽略其它在 end 前的條件與動作。如果所有條件都測試失敗,則會產(chǎn)生運行時錯誤。一個測試恒為真的條件就是 true。它常用作 if 的最后一個條件,即當所有條件都測試失敗時,則執(zhí)行 true 后面的動作。

下面這個例子說明了 if 的工作方式:

-module(tut9).
-export([test_if/2]).

test_if(A, B) ->
    if 
        A == 5 ->
            io:format("A == 5~n", []),
            a_equals_5;
        B == 6 ->
            io:format("B == 6~n", []),
            b_equals_6;
        A == 2, B == 3 ->                      %That is A equals 2 and B equals 3
            io:format("A == 2, B == 3~n", []),
            a_equals_2_b_equals_3;
        A == 1 ; B == 7 ->                     %That is A equals 1 or B equals 7
            io:format("A == 1 ; B == 7~n", []),
            a_equals_1_or_b_equals_7
    end.

測試該程序:

60> c(tut9).
{ok,tut9}
61> tut9:test_if(5,33).
A == 5
a_equals_5
62> tut9:test_if(33,6).
B == 6
b_equals_6
63> tut9:test_if(2, 3).
A == 2, B == 3
a_equals_2_b_equals_3
64> tut9:test_if(1, 33).
A == 1 ; B == 7
a_equals_1_or_b_equals_7
65> tut9:test_if(33, 7).
A == 1 ; B == 7
a_equals_1_or_b_equals_7
66> tut9:test_if(33, 33).
** exception error: no true branch found when evaluating an if expression
     in function  tut9:test_if/2 (tut9.erl, line 5)

注意,tut9:test_if(33,33) 使得所有測試條件都失敗,這將導致產(chǎn)生一個 if_clause 運行時錯誤。參考 Guard 序列 可以得到更多關(guān)于 guard 測試的內(nèi)容。

Erlang 中還有一種 case 結(jié)構(gòu)?;叵胍幌虑懊娴?convert_length 函數(shù):

convert_length({centimeter, X}) ->
    {inch, X / 2.54};
convert_length({inch, Y}) ->
    {centimeter, Y * 2.54}.

該函數(shù)也可以用 case 實現(xiàn),如下所示:

-module(tut10).
-export([convert_length/1]).

convert_length(Length) ->
    case Length of
        {centimeter, X} ->
            {inch, X / 2.54};
        {inch, Y} ->
            {centimeter, Y * 2.54}
    end.

無論是 case 還是 if 都有返回值。這也就是說,上面的例子中,case 語句要么返回 {inch,X/2.54} 要么返回 {centimeter,Y*2.54}。case 語句也可以用 guard 子句來實現(xiàn)。下面的例子可以幫助你分清二者。這個例子中,輸入年份得到指定某月的天數(shù)。年份必須是已知的,因為閏年的二月有 29 天,所以必須根據(jù)年份才能判斷二月的天數(shù)。

-module(tut11).
-export([month_length/2]).

month_length(Year, Month) ->
    %% 被 400 整除的為閏年。
    %% 被 100 整除但不能被 400 整除的不是閏年。
    %% 被 4 整除但不能被 100 整除的為閏年。
    Leap = if
        trunc(Year / 400) * 400 == Year ->
            leap;
        trunc(Year / 100) * 100 == Year ->
            not_leap;
        trunc(Year / 4) * 4 == Year ->
            leap;
        true ->
            not_leap
    end,  
    case Month of
        sep -> 30;
        apr -> 30;
        jun -> 30;
        nov -> 30;
        feb when Leap == leap -> 29;
        feb -> 28;
        jan -> 31;
        mar -> 31;
        may -> 31;
        jul -> 31;
        aug -> 31;
        oct -> 31;
        dec -> 31
    end
70> c(tut11).
{ok,tut11}
71> tut11:month_length(2004, feb).
29
72> tut11:month_length(2003, feb).
28
73> tut11:month_length(1947, aug).
31