Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Erlang if and case


May 13, 2021 Erlang


Table of contents


Erlang if and case

The above find_max_and_min to find the maximum and minimum values of temperature. H ere's a new structure if. I f's syntax format is as follows:

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

Note that there is no "before end"; ” 。 C onditions work the same way as guard, i.e. test and return success or failure. E rlang starts the test from the first condition until he finds a true branch of the test. T he actions after the condition are then performed, ignoring the other conditions and actions before end. R un-time errors occur if all conditions fail the test. O ne condition in which a test is constant is true. I t is often used as the last condition of if, i.e. when all conditions fail to test, the action after true is performed.

Here's an example of how if works:

-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.

Test the program:

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)

Note that tut9:test_if(33,33) all test conditions to fail, which results in if_clause runtime error. R efer to the Guard sequence for more information about the guard test.

There is also a case structure in Erlang. T hink back to the previous convert_length function:

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

The function can also be implemented with case, as follows:

-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.

Both case and if have return values. T his means that, in the example above, the case statement either returns either s inch, X/2.54, or back to s {centimeter,Y*2.54} C ase statements can also be implemented with the guard clause. T he following examples can help you tell the two. I n this example, the input year gets the number of days for a specified month. T he year must be known because February of a leap year has 29 days, so the number of days in February must be determined based on the year.

-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