我爱编程Erlang

erlang-基本语法_3、原子类型atom、元组、列表、if与

2018-06-14  本文已影响2人  zhang_yongfeng

写在前面

本文主要是以一个erlang的学习记录,有错误的地方欢迎指正.


erlang学习脑图

原子类型

原子类型是 Erlang 语言中另一种数据类型。所有原子类型都以小写字母开头
示例:
新建tut2.erl,代码如下;

-module(tut2).
-export([convert/2]).

%完成英寸与厘米之间的相互转换
%完成厘米转为英寸
convert(M,inch) ->
    M / 2.54;

%%完成英寸转为厘米
convert(M, centimeter) ->
    M * 2.54.

在命令窗口中如下:

%编译:
20> c(tut2).             
{ok,tut2}
%测试:
21> tut2:convert(3,inch).
1.1811023622047243
22> tut2:convert(3,centimeter).
7.62
23> tut2:convert(3,other).
%Erlang 系统找不到匹配的子句,所以返回了错误消息 function_clause
** exception error: no function clause matching tut2:convert(3,centimeter4567) (tut2.erl, line 4)
24> 

元组

上一示例中
tut2.convert(3,inch)

这是意味着 3 本身已经是英寸表示了呢?还是指将 3 厘米转换成英寸呢? Erlang 提供了将某些元素分成组并用以更易于理解的方式表示的机制。它就是元组。一个元组由花括号括起来的。所以,{inch,3} 指的就是 3 英寸,而 {centimeter, 5} 指的就是 5 厘米。接下来,我们将重写厘米与英寸之间的转换程序。将下面的代码输入到文件 tut3.erl 文件中:

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

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

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

在命令窗口中如下:

7> tut3:convert_length({inch,5}).
{centimeter,12.7}
8> tut3:convert_length(tut3:convert_length({inch,5}).
%%少输了一个括号导致报错
* 1: syntax error before: '.'
8> tut3:convert_length(tut3:convert_length({inch,5})).
{inch,5.0}
9> 

列表list

虽然元组可以将数据组成一组,但是我们也需要表示数据列表。 Erlang中的列表由方括号括起来表示。
示例1:世界上不同城市的温度列表就可以表示为:

1> [{moscow, {c, -10}},{cape_town,{f,70}},{stockholm,{c,-14}}]
1> .
[{moscow,{c,-10}},{cape_town,{f,70}},{stockholm,{c,-14}}]
2> 

示例2:使用 “|” 查看部分列表

2> [First | TheRest]=[1,2,3,4].
[1,2,3,4]
%每间隔一个| 对应一个元素
3> First.
1
4> TheRest.
[2,3,4]
5> 

示例3:使用“|” 对应多个元素

5> [E1, E2 | R ] = [1,2,3,4,5,6,7]. 
[1,2,3,4,5,6,7]
6> E1.
1
7> E2.
2
8> R.
[3,4,5,6,7]
9> 

示例4:取值超过列表中元素的总数

10> [A, B| C]= [1, 2].
[1,2]
11> A.
1
12> B.
2
13> C.
[]
%取不到值时会返回一个空的列表
14> 

示例5:获得一个列表的长度,
新建一个tut4.erl文件,代码如下:

-module(tut4).

-export([list_length/1]).

list_length([]) ->
    0;

list_length([First | Rest]) ->
    1 + list_length(Rest).
%编译
1> c(tut4).
tut4.erl:8: Warning: variable 'First' is unused
{ok,tut4}

%运行
3> tut4:list_length([1,2,3,4]).
4
4> 

示例6:在 Erlang 中字符串可以用 Unicode 字符的列表表示

4> [97,98,99].
"abc"
5> 

Erlang if 与 case

语法格式:

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

注意,在 end 之前没有 “;”。条件(Condidtion)的工作方式与 guard 一样,即测试并返回成功或者失败。

示例1:

-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 ->
            io:format("A == 2, B == 3~n",[]),
            a_equals_2_and_b_equals_3;
        A == 1; B == 7 ->
            io:format("A == 1; B== 7~n",[]),
            a_equals_1_or_b_equals_7    
    end.


%运行
9> tut9:test_if(5,[]).
A == 5
a_equals_5
10> tut9:test_if([],6).
B == 6
b_equals_6
11> tut9:test_if(2,3). 
A == 2, B == 3
a_equals_2_b_equals_3
12> tut9:test_if(1,[]).
A == 1; B== 7
a_equals_1_or_b_equals_7
13> tut9:test_if([],7). 
A == 1; B== 7
a_equals_1_or_b_equals_7
14> tut9:test_if([],[]).
** exception error: no true branch found when evaluating an if expression
     in function  tut9:test_if/2 (tut9.erl, line 6)
     %%tut9:test_if([],[]) 使得所有测试条件都失败,这将导致产生一个 if_clause 运行时错误
15> 

示例2:输入年份得到指定某月的天数
新建tut11.erl 代码如下:

-module(tut11).

-export([month_length/2]).

month_length(Year,Month) ->
    %% 被 400 整除的为闰年。
    %% 被 100 整除但不能被 400 整除的不是闰年。
    %% 被 4 整除但不能被 100 整除的为闰年。
    Leap = if 
        trunc(Year / 400) * 400 == Year ->
        %Year取整除400 再乘400 等于Year,说明能被400整除
            leap;
        trunc(Year / 100) * 100 == Year ->
            not_leap;
        trunc(Year / 4) * 4 == Year ->
            leap;
        true ->
            not_leap
    end,

    case Month of
        apr -> 30;%四月
        jun -> 30;%六月
        sept -> 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.
%编译
21> c(tut11).
{ok,tut11} 
22> tut11:month_length(2004,feb).
29
23> tut11:month_length(2003,feb).
28
24> tut11:month_length(1947,feb).
28
25> tut11:month_length(17,feb).  
28

写在后面

引用网络博客内容:
https://www.jianshu.com/p/b45eb9314d1e (30 分钟学 Erlang (一))
https://www.w3cschool.cn/erlang/tohb1p5z.html (w3cschool erlang教程)

个人博客地址:https://zhangyongfeng1.github.io/
简书地址:https://www.jianshu.com/u/137f325832fb

上一篇 下一篇

猜你喜欢

热点阅读