[erlang-questions] Idiomatic Erlang, style/performance question
Wed Jun 18 22:30:34 CEST 2008
Is there a styleguide somewhere on Erlang? Im currently going through the Erlang reference manual and you can see some examples, are those generally referred to as idiomatic Erlang?
Some examples of implementations of the power-function below.
The last function someone on the mailing list provided as a correction to the second one.
The first one uses pattern matching and guard sequences right? Is this a good way? I dont quite understand the advantage of the last function.
However, the shell complains about
../mymath.erl:24: Warning: variable 'N' is unused
Which is the powerx(N, 0) -> 1; line.
However i saw some code that used _ and i tried:
powerx(_, 0) -> 1;
so _ is the correct way to write this function?
Hmm while writing I got a thought. Is the last function tail-recursive? I have only heard that term but never used it.
Is tail-recursion faster or uses less memory, what is the advantage/disadvantage?
powerx(N, 0) -> 1;
powerx(N, P) when is_integer(P), P > 0 ->
N * powerx(N, P - 1);
powerx(N, P) when is_integer(P), P < 0 ->
1 / powerx(N, -1 * P).
powerc(N, P) ->
P > 0 ->
N * powerc(N, P - 1);
P < 0 ->
1 / powerc(N, -1 * P);
P == 0 ->
power(N, P) when is_integer(N), N >= 0 ->
ipower(N, P, 1);
power(N, P) when is_integer(N), N < 0 ->
1/ipower(-N, P, 1).
ipower(0, _, R) -> R;
ipower(N, P, R) -> ipower(N-1, P, R*P).
Ta semester! - sök efter resor hos Yahoo! Shopping.
Jämför pris på flygbiljetter och hotellrum här:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions