UP | HOME

列表操作

Table of Contents

| 操作符

| 操作符可以用于取列表中的首元素:

1> [M1|T1] = [paris, london, rome].
[paris,london,rome]

2> M1.
paris

3> T1.
[london,rome]

| 操作符也可以用于在列表 首部添加元素 :

5>  L1 = [madrid | T1]. 
[madrid,london,rome]

6> L1 . 
[madrid,london,rome] 

使用 | 操作符 翻转列表中的元素 :

-module(reverse).

-export([reverse/1]).

reverse(List) ->
    reverse(List, []).

reverse([Head | Rest], Reversed_List) ->
    reverse(Rest, [Head | Reversed_List]);
reverse([], Reversed_List) ->
    Reversed_List.

测试如下:

3> reverse:reverse([1,2,3]).
[3,2,1]

初始时 Reversed_List 其为 [],待翻转的列表的首元素被取出来再添加到 Reversed_List 列表中:

reverse([1|2,3], []) =>
    reverse([2,3], [1|[]])

reverse([2|3], [1]) =>
    reverse([3], [2|[1])

reverse([3|[]], [2,1]) =>
    reverse([], [3|[2,1]])

reverse([], [3,2,1]) =>
    [3,2,1]

实例

下面回到城市与温度的话题上,但是这一次会使用更加结构化的方法

首先,将整个列表中的温度都使用摄氏度表示:

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

format_temps(List_of_cities) ->
    convert_list_to_c(List_of_cities).

convert_list_to_c([{Name, {f, F}} | Rest]) ->
    Converted_City = {Name, {c, (F -32)* 5 / 9}},
    [Converted_City | convert_list_to_c(Rest)];

convert_list_to_c([City | Rest]) ->
    [City | convert_list_to_c(Rest)];

convert_list_to_c([]) ->
    [].

含义如下:

format_temps(List_of_cities) ->
    convert_list_to_c(List_of_cities).

format_temps/1 调用 convert_list_to_c/1 函数。covert_list_to_c/1 函数移除 List_of_cities 的首元素,并将其转换为摄氏单位表示 (如果需要)。| 操作符用来将被转换后的元素添加到转换后的剩余列表中:

[Converted_City | convert_list_to_c(Rest)];

或者:

[City | convert_list_to_c(Rest)];

一直重复上述过程直到列表空为止。当列表为空时,则执行:

convert_list_to_c([]) ->
    [].

测试下:

2> temperature:format_temps([{moscow, {c, -10}}, {cape_town, {f, 70}},
2> {stockholm, {c, -4}}, {paris, {f, 28}}, {london, {f, 36}}]) .      

[{moscow,{c,-10}},
 {cape_town,{c,21.11111111111111}},
 {stockholm,{c,-4}},
 {paris,{c,-2.2222222222222223}},
 {london,{c,2.2222222222222223}}]

当列表被转换后,用新增的打印输出函数将其输出:

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

format_temps(List_of_cities) ->
    % convert_list_to_c(List_of_cities).
    Converted_List = convert_list_to_c(List_of_cities),
    print_temp(Converted_List).

convert_list_to_c([{Name, {f, F}} | Rest]) ->
    Converted_City = {Name, {c, (F -32)* 5 / 9}},
    [Converted_City | convert_list_to_c(Rest)];

convert_list_to_c([City | Rest]) ->
    [City | convert_list_to_c(Rest)];

convert_list_to_c([]) ->
    [].

print_temp([{Name, {c, Temp}} | Rest]) ->
    io:format("~-15w ~w c~n", [Name, Temp]),
    print_temp(Rest);
print_temp([]) ->
    ok.
4> temperature:format_temps([{moscow, {c, -10}}, {cape_town, {f, 70}},
4> {stockholm, {c, -4}}, {paris, {f, 28}}, {london, {f, 36}}]) .      

moscow          -10 c
cape_town       21.11111111111111 c
stockholm       -4 c
paris           -2.2222222222222223 c
london          2.2222222222222223 c
ok

接下来,添加一个函数来搜索拥有最高温度与最低温度值的城市:

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

format_temps(List_of_cities) ->
    Converted_List = convert_list_to_c(List_of_cities),
    print_temp(Converted_List),
    {Max_city, Min_city} = find_max_and_min(Converted_List),
    print_max_and_min(Max_city, Min_city).

convert_list_to_c([{Name, {f, F}} | Rest]) ->
    Converted_City = {Name, {c, (F -32)* 5 / 9}},
    [Converted_City | convert_list_to_c(Rest)];

convert_list_to_c([City | Rest]) ->
    [City | convert_list_to_c(Rest)];

convert_list_to_c([]) ->
    [].

print_temp([{Name, {c, Temp}} | Rest]) ->
    io:format("~-15w ~w c~n", [Name, Temp]),
    print_temp(Rest);
print_temp([]) ->
    ok.

find_max_and_min([City | Rest]) ->
    find_max_and_min(Rest, City, City).

find_max_and_min([{Name, {c, Temp}} | Rest], 
                 {Max_Name, {c, Max_Temp}}, 
                 {Min_Name, {c, Min_Temp}}) ->
    if 
        Temp > Max_Temp ->
            Max_City = {Name, {c, Temp}};           % Change
        true -> 
            Max_City = {Max_Name, {c, Max_Temp}} % Unchanged
    end,
    if
        Temp < Min_Temp ->
            Min_City = {Name, {c, Temp}};           % Change
        true -> 
            Min_City = {Min_Name, {c, Min_Temp}} % Unchanged
    end,
    find_max_and_min(Rest, Max_City, Min_City);

find_max_and_min([], Max_City, Min_City) ->
    {Max_City, Min_City}.

print_max_and_min({Max_name, {c, Max_temp}}, {Min_name, {c, Min_temp}}) ->
    io:format("Max temperature was ~w c in ~w~n", [Max_temp, Max_name]),
    io:format("Min temperature was ~w c in ~w~n", [Min_temp, Min_name]).

6> temperature:format_temps([{moscow, {c, -10}}, {cape_town, {f, 70}},
6> {stockholm, {c, -4}}, {paris, {f, 28}}, {london, {f, 36}}]) .      

moscow          -10 c
cape_town       21.11111111111111 c
stockholm       -4 c
paris           -2.2222222222222223 c
london          2.2222222222222223 c
Max temperature was 21.11111111111111 c in cape_town
Min temperature was -10 c in moscow
现在的代码并不是最高效的方式,因为它遍历了四次列表

但是首先应当保证程序的清晰性和正确性,然后才是想办法提高程序的效率

Next:条件判断

Previous:匹配和作用域

Home:顺序编程