<table cellspacing='0' cellpadding='0' border='0' ><tr><td valign='top' style='font: inherit;'>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?<br><br>Some examples of implementations of the power-function below.<br>The last function someone on the mailing list provided as a correction to the second one.<br>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.<br>However, the shell complains about <br>88> c(mymath).<br>./mymath.erl:24: Warning: variable 'N' is unused<br>{ok,mymath}<br>Which is the powerx(N, 0) -> 1; line.<br>However i saw some code that used _ and i tried:<br>powerx(_, 0) -> 1;<br>so _ is the correct way to write this function?<br><br>Hmm while writing I got a thought. Is the last function tail-recursive? I
have only heard that term but never used it. <br>Is tail-recursion faster or uses less memory, what is the advantage/disadvantage?<br><br>powerx(N, 0) -> 1;<br>powerx(N, P) when is_integer(P), P > 0 -><br> N * powerx(N, P - 1);<br>powerx(N, P) when is_integer(P), P < 0 -><br> 1 / powerx(N, -1 * P).<br> <br>powerc(N, P) -><br> if <br> P > 0 -><br> N * powerc(N, P - 1);<br> P < 0
-><br> 1 / powerc(N, -1 * P);<br> P == 0 -><br> 1<br> end.<br><br>power(N, P) when is_integer(N), N >= 0 -><br> ipower(N, P, 1);<br> power(N, P) when is_integer(N), N < 0 -><br> 1/ipower(-N, P, 1).<br><br> ipower(0, _, R) -> R;<br> ipower(N, P, R) -> ipower(N-1, P, R*P).</td></tr></table><br>
<hr size=1>
Går det långsamt? Skaffa dig en snabbare bredbandsuppkoppling.<br>
<a href=http://shopping.yahoo.se/c-100015813-bredband.html?partnerId=96914325>Sök och jämför hos Yahoo! Shopping.</a>