[erlang-questions] Idiomatic Erlang, style/performance question

Circular Function circularfunc@REDACTED
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 
88> c(mymath).
../mymath.erl:24: Warning: variable 'N' is unused
{ok,mymath}
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) ->
        if 
                P > 0 ->
                        N * powerc(N, P - 1);
                P < 0 ->
                        1 / powerc(N, -1 * P);
                P == 0 ->
                        1
        end.

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:
http://shopping.yahoo.se/c-169901-resor-biljetter.html?partnerId=96914052
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080618/24d3e830/attachment.htm>


More information about the erlang-questions mailing list