该函数应该计算2个整数N和M之间的整数之和,其中N <= M。
sum(N, M) ->
if
N+1 =< M -> N + sum((N+1), M);
N =< M -> N;
N > M -> 'N must be <= M'
end.
有更好的方法吗?我可以把第二个陈述放在第一个吗?
答案 0 :(得分:2)
这个怎么样?
sum(N, M) when N > M ->
{error, 'N must be <= M'};
sum(N, M) ->
List = lists:seq(N, M),
lists:foldl(fun(X, Sum) -> X + Sum end, 0, List).
最好使用尾递归,并保证在最佳实践中验证某些内容。
答案 1 :(得分:2)
一些评论和主张
-module (ex).
-compile([export_all]).
%% some comments on your proposal
sum1(N, M) ->
if %% if statement is not very popular in Erlang since it is not realy an if like in java or C
N+1 =< M -> N + sum1((N+1), M);
N =< M -> N;
N > M -> 'N must be <= M' %% don't do this, it creates useless atoms that are limited
%% in number and cannot be destroyed during the VM life
end.
%% second solution, tail recursive
sum2(N,M) when N > M -> {error,"N must be <= M"}; %% instead use a guard an return a tuple with
%% standard atom + a string for the reason
sum2(N,M) -> sum2(N,M,0). %% use an accumulator to create a tail recursive function
%% not really important for this example but it is a good usage
%% to avoid stack overflow :o)
sum2(M,M,R) -> M+R;
sum2(N,M,R) -> sum2(N+1,M,R+N). %% like this it is tail recursive.
%% third solution, without recursion: don't use recursive function if it is not necessary
sum3(M,M) -> M;
sum3(0,M) -> sum3(1,M);
sum3(N,M) when N =< M -> %% don't be defensive unless your function is likely subject to
%% receive bad argument from user intreface
(M*(M+1)-(N-1)*N) div 2.