Content deleted Content added
→Fibonacci sequence: copyedit comments and remove redundant rules from the shortened program |
→Fibonacci sequence: avoid unnecessary addition |
||
Line 121:
%% The order in which these function signatures are declared is a vital
%% part of this module's functionality
fib(0) -> 0;▼
%% If fib/1 receives a negative number, then return the atom err_neg_val
%% Normally, such defensive coding is discouraged due to Erlang's 'Let
%% it Crash' philosophy
%% loop.
fib(N) when N < 0 -> err_neg_val;
%% If fib/1 is passed
▲fib(0) -> 0;
fib(N) when N < 3 -> 1;▼
%% For all other values, call the private function fib_int/3 to perform
%% the calculation
fib(N) -> fib_int(N-1, 0, 1).
%% ---------------------------------------------------------------------
Line 145 ⟶ 140:
%% If fib_int/3 receives 0 as its first argument, then we're done, so
%% return the value in argument
%% to disregard its value.
fib_int(0,
%% For all other argument combinations, recursively call fib_int/3
Line 158 ⟶ 153:
</syntaxhighlight>
Omitting the comments
<syntaxhighlight lang="erlang">
Line 164 ⟶ 159:
-export([fib/1]).
fib(N) when N < 0 -> err_neg_val;
fib(N) -> fib_int(N-1, 0, 1).
fib_int(0,
fib_int(N, A, B) -> fib_int(N-1, B, A+B).
</syntaxhighlight>
|