erlang技术2014 3 28 复合函数

Test  = fun(X) -> (fun(Y) -> Y * X end) end.

Test(3).   //定义了外层的fun(X).

Result = Test(3).

Result(2). //定义了fun(Y).

 

 

 

%shop.erl

cost(apple)    ->1;

cost(orange)  ->2;

cost(peach)   ->3;

cost(pear)     ->4;

cost(banana) ->5.

 

%shop2.erl

-import(lists,[map/2,sum/1]).

total(L) -> sum(map(fun(What) -> shop:cost(What) end,L)).

 

%fun(What) -> shop:cost(What) end.  shop:cost(What) 作为函数的返回值。

%map(F,L)  这里的F指的是上面的一句话。

%sum(List)。 这里的List = map(F,L).

 

 

 

 

posted on 2014-03-28 14:51  Vinlan  阅读(96)  评论(0)    收藏  举报

导航