Erlang 列表

Erlang 更多关于列表的内容

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

  1. 47> [M1|T1] = [paris, london, rome].
  2. [paris,london,rome]
  3. 48> M1.
  4. paris
  5. 49> T1.
  6. [london,rome]

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

  1. 50> L1 = [madrid | T1].
  2. [madrid,london,rome]
  3. 51> L1.
  4. [madrid,london,rome]

使用 | 操作符操作列表的例子如下 — 翻转列表中的元素:

  1. -module(tut8).
  2. -export([reverse/1]).
  3. reverse(List) ->
  4. reverse(List, []).
  5. reverse([Head | Rest], Reversed_List) ->
  6. reverse(Rest, [Head | Reversed_List]);
  7. reverse([], Reversed_List) ->
  8. Reversed_List.
  1. 52> c(tut8).
  2. {ok,tut8}
  3. 53> tut8:reverse([1,2,3]).
  4. [3,2,1]

仔细捉摸一下,Reversed_List 是如何被创建的。初始时,其为 []。随后,待翻转的列表的首元素被取出来再添加到 Reversed_List 列表中,如下所示:

  1. reverse([1|2,3], []) =>
  2. reverse([2,3], [1|[]])
  3. reverse([2|3], [1]) =>
  4. reverse([3], [2|[1])
  5. reverse([3|[]], [2,1]) =>
  6. reverse([], [3|[2,1]])
  7. reverse([], [3,2,1]) =>
  8. [3,2,1]

lists 模块中包括许多操作列表的函数,例如,列表翻转。所以,在自己动手写操作列表的函数之前是可以先检查是否在模块中已经有了(参考 STDLIB 中 lists(3) 手册)。

下面让我们回到城市与温度的话题上,但是这一次我们会使用更加结构化的方法。首先,我们将整个列表中的温度都使用摄氏度表示:

  1. -module(tut7).
  2. -export([format_temps/1]).
  3. format_temps(List_of_cities) ->
  4. convert_list_to_c(List_of_cities).
  5. convert_list_to_c([{Name, {f, F}} | Rest]) ->
  6. Converted_City = {Name, {c, (F -32)* 5 / 9}},
  7. [Converted_City | convert_list_to_c(Rest)];
  8. convert_list_to_c([City | Rest]) ->
  9. [City | convert_list_to_c(Rest)];
  10. convert_list_to_c([]) ->

测试一下上面的函数:

  1. 54> c(tut7).
  2. {ok, tut7}.
  3. 55> tut7:format_temps([{moscow, {c, -10}}, {cape_town, {f, 70}},
  4. {stockholm, {c, -4}}, {paris, {f, 28}}, {london, {f, 36}}]).
  5. [{moscow,{c,-10}},
  6. {cape_town,{c,21.11111111111111}},
  7. {stockholm,{c,-4}},
  8. {paris,{c,-2.2222222222222223}},
  9. {london,{c,2.2222222222222223}}]

含义如下:

  1. format_temps(List_of_cities) ->
  2. convert_list_to_c(List_of_cities).

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

  1. [Converted_City | convert_list_to_c(Rest)];

或者:

  1. [City | convert_list_to_c(Rest)];

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

  1. convert_list_to_c([]) ->
  2. [].

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

  1. -module(tut7).
  2. -export([format_temps/1]).
  3. format_temps(List_of_cities) ->
  4. Converted_List = convert_list_to_c(List_of_cities),
  5. print_temp(Converted_List).
  6. convert_list_to_c([{Name, {f, F}} | Rest]) ->
  7. Converted_City = {Name, {c, (F -32)* 5 / 9}},
  8. [Converted_City | convert_list_to_c(Rest)];
  9. convert_list_to_c([City | Rest]) ->
  10. [City | convert_list_to_c(Rest)];
  11. convert_list_to_c([]) ->
  12. [].
  13. print_temp([{Name, {c, Temp}} | Rest]) ->
  14. io:format("~-15w ~w c~n", [Name, Temp]),
  15. print_temp(Rest);
  16. print_temp([]) ->
  17. ok.
  1. 56> c(tut7).
  2. {ok,tut7}
  3. 57> tut7:format_temps([{moscow, {c, -10}}, {cape_town, {f, 70}},
  4. {stockholm, {c, -4}}, {paris, {f, 28}}, {london, {f, 36}}]).
  5. moscow -10 c
  6. cape_town 21.11111111111111 c
  7. stockholm -4 c
  8. paris -2.2222222222222223 c
  9. london 2.2222222222222223 c
  10. ok

接下来,添加一个函数来搜索拥有最高温度与最低温度值的城市。下面的方法并不是最高效的方式,因为它遍历了四次列表。但是首先应当保证程序的清晰性和正确性,然后才是想办法提高程序的效率:

  1. -module(tut7).
  2. -export([format_temps/1]).
  3. format_temps(List_of_cities) ->
  4. Converted_List = convert_list_to_c(List_of_cities),
  5. print_temp(Converted_List),
  6. {Max_city, Min_city} = find_max_and_min(Converted_List),
  7. print_max_and_min(Max_city, Min_city).
  8. convert_list_to_c([{Name, {f, Temp}} | Rest]) ->
  9. Converted_City = {Name, {c, (Temp -32)* 5 / 9}},
  10. [Converted_City | convert_list_to_c(Rest)];
  11. convert_list_to_c([City | Rest]) ->
  12. [City | convert_list_to_c(Rest)];
  13. convert_list_to_c([]) ->
  14. [].
  15. print_temp([{Name, {c, Temp}} | Rest]) ->
  16. io:format("~-15w ~w c~n", [Name, Temp]),
  17. print_temp(Rest);
  18. print_temp([]) ->
  19. ok.
  20. find_max_and_min([City | Rest]) ->
  21. find_max_and_min(Rest, City, City).
  22. find_max_and_min([{Name, {c, Temp}} | Rest],
  23. {Max_Name, {c, Max_Temp}},
  24. {Min_Name, {c, Min_Temp}}) ->
  25. if
  26. Temp > Max_Temp ->
  27. Max_City = {Name, {c, Temp}}; % Change
  28. true ->
  29. Max_City = {Max_Name, {c, Max_Temp}} % Unchanged
  30. end,
  31. if
  32. Temp < Min_Temp ->
  33. Min_City = {Name, {c, Temp}}; % Change
  34. true ->
  35. Min_City = {Min_Name, {c, Min_Temp}} % Unchanged
  36. end,
  37. find_max_and_min(Rest, Max_City, Min_City);
  38. find_max_and_min([], Max_City, Min_City) ->
  39. {Max_City, Min_City}.
  40. print_max_and_min({Max_name, {c, Max_temp}}, {Min_name, {c, Min_temp}}) ->
  41. io:format("Max temperature was ~w c in ~w~n", [Max_temp, Max_name]),
  42. io:format("Min temperature was ~w c in ~w~n", [Min_temp, Min_name]).
  1. 58> c(tut7).
  2. {ok, tut7}
  3. 59> tut7:format_temps([{moscow, {c, -10}}, {cape_town, {f, 70}},
  4. {stockholm, {c, -4}}, {paris, {f, 28}}, {london, {f, 36}}]).
  5. moscow -10 c
  6. cape_town 21.11111111111111 c
  7. stockholm -4 c
  8. paris -2.2222222222222223 c
  9. london 2.2222222222222223 c
  10. Max temperature was 21.11111111111111 c in cape_town
  11. Min temperature was -10 c in moscow
  12. ok