shop.erl
%% Author: Administrator
%% Created: 2012-8-23
%% Description: TODO: Add description to shop
-module(shop).
%%
%% Include files
%%
%%
%% Exported Functions
%%
-export([cost/1]).
%%
%% API Functions
%%
cost(oranges)
->5;
cost(newspaper)
->8;
cost(apples)
->2;
cost(pears)
->9;
cost(milk)
->7.
%%
%% Local Functions
%%
shop1.erl
%% Author: Administrator
%% Created: 2012-8-23
%% Description: TODO: Add description to shop1
-module(shop1).
%%
%% Include files
%%
%%
%% Exported Functions
%%
-export([total/1]).
%%
%% API Functions
%%
total([{What,N}|T])
->shop:cost(What)*N+total(T);
total([])
->0.
%%
%% Local Functions
%%
cd("/mnt/web/source_erlang/demo1/src").
c(shop).
c(shop1).
Buys=[{oranges,4},{newspaper,1},{apples,10},{pears,6},{milk,3}].
shop1:total(Buys).
这点代码很有意思,递归是必须的,主要是在shop:cost(What)*N+total(T)
T是列表的后面的数据,依次递归下去,直至为空,当为空时变成匹配total([])方法
执行方式应该是
oranges*4+{newspaper,1},{apples,10},{pears,6},{milk,3}=40+{newspaper,1},{apples,10},{pears,6},{milk,3}
40+newspaper*1=40+8+{apples,10},{pears,6},{milk,3}
一直下去,最后变成了
40+8+2*10+9*6+7*3+total([]) //total([])=0
ps:尼玛,这也太精简了,python哪能和这语言相比,不过就是真TMD难懂,递归精简,我擦了.