Erlang 作为函数式编程语言自然拥有高阶函数。
1. 例如定义一个数值翻倍的函数如下:
%%%-------------------------------------------------------------------
%%% @author Administrator
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 08. 一月 2019 上午 10:55
%%%-------------------------------------------------------------------
-module(helloworld).
-author("Administrator").
%% API
-export([start/0]).
%%-export([format_temps/1]).
start() ->
Xf = fun(X) -> X * 2 end,
io:format("The result is : ~p ~n", [Xf(6)]).
运行结果:
2. Erlang 有两个非常有用的操作列表的函数 foreach 与 map, 定义如下:
foreach(Fun, [First|Rest]) ->
Fun(First),
foreach(Fun, Rest);
foreach(Fun, []) ->
ok.
map(Fun, [First|Rest]) ->
[Fun(First)|map(Fun,Rest)];
map(Fun, []) ->
[].
这两个函数是由标准模块 lists 提供的。foreach 将一个函数作用于列表中的每一个元素。 map 通过将一个函数作用于列表中的每个元素生成一个新的列表。
%%%-------------------------------------------------------------------
%%% @author Administrator
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 08. 一月 2019 上午 10:55
%%%-------------------------------------------------------------------
-module(helloworld).
-author("Administrator").
%% API
-export([start/0]).
%%-export([format_temps/1]).
start() ->
Xf = fun(X) -> X * 2 end,
Add_3 = fun(X) -> X + 3 end,
io:format("The result is : ~p ~n", [lists:map(Add_3, [1,2,3])]).
运行结果:
输出一组城市的温度:
%%%-------------------------------------------------------------------
%%% @author Administrator
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 08. 一月 2019 上午 10:55
%%%-------------------------------------------------------------------
-module(helloworld).
-author("Administrator").
%% API
-export([start/0]).
%%-export([format_temps/1]).
start() ->
Print_City = fun({City, {X, Temp}}) -> io:format("~-15w ~w ~w~n", [City, X, Temp]) end,
io:format("The result is : ~p ~n", [lists:foreach(Print_City, [{moscow, {c, -10}}, {cape_town, {f, 70}}, {stockholm, {c, -4}},
{paris, {f, 28}}, {london, {f, 36}}])]).
运行结果:
下面定义一个函数,这个函数用于遍历城市温度列表并将每个温度值都转换为摄氏温度表示:
%%%-------------------------------------------------------------------
%%% @author Administrator
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 08. 一月 2019 上午 10:55
%%%-------------------------------------------------------------------
-module(helloworld).
-author("Administrator").
%% API
-export([start/0]).
%%-export([format_temps/1]).
start() ->
io:format("The result is : ~p ~n", [convert_list_to_c([{moscow, {c, -10}}, {cape_town, {f, 70}},
{stockholm, {c, -4}}, {paris, {f, 28}}, {london, {f, 36}}])]).
convert_to_c({Name, {f, Temp}}) ->
{Name, {c, trunc((Temp - 32) * 5 / 9)}};
convert_to_c({Name, {c, Temp}}) ->
{Name, {c, Temp}}.
convert_list_to_c(List) ->
lists:map(fun convert_to_c/1, List).
运行结果:
lists 标准库中还包括排序函数 sort(Fun,List),其中 Fun 接受两个输入参数,如果第一个元素比第二个元素小则函数返回真,否则返回假。
%%%-------------------------------------------------------------------
%%% @author Administrator
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 08. 一月 2019 上午 10:55
%%%-------------------------------------------------------------------
-module(helloworld).
-author("Administrator").
%% API
-export([start/0]).
%%-export([format_temps/1]).
start() ->
io:format("The result is : ~p ~n", [convert_list_to_c([{moscow, {c, -10}}, {cape_town, {f, 70}},
{stockholm, {c, -4}}, {paris, {f, 28}}, {london, {f, 36}}])]).
convert_to_c({Name, {f, Temp}}) ->
{Name, {c, trunc((Temp - 32) * 5 / 9)}};
convert_to_c({Name, {c, Temp}}) ->
{Name, {c, Temp}}.
convert_list_to_c(List) ->
New_list = lists:map(fun convert_to_c/1, List),
lists:sort(fun({_, {c, Temp1}}, {_, {c, Temp2}}) ->
Temp1 < Temp2 end, New_list).
运行结果:
这儿用到了匿名变量 "_" 的概念。匿名变量常用于忽略一个获得的变量值的场景下。