[erlang-questions] power(N,P), cant get it to work

Torben Hoffmann torben.lehoff@REDACTED
Tue Jun 17 10:13:12 CEST 2008


1. You have to end your if's with an end - see
http://www.erlang.org/doc/reference_manual/expressions.html#6.7
2. Consider re-writing power using pattern matching and use guard sequences
(quite close to the above link in the reference manaul) to make sure that
the input data is okay, e.g., math:power(2,0.5) will give you an infinite
loop.

Cheers,
Torben

2008/6/17 Circular Function <circularfunc@REDACTED>:

> -module(math).
> -export([fac/1, sq/1,cube/1,power/2]).
>
> fac(N) when N>0 ->
>         N * fac(N-1);
> fac(0) ->
>         1.
>
> sq(X) -> X*X.
>
> cube(X) -> X*X*X.
>
> power(N,P) ->
>         if P==0 ->
>                 1;
>         true ->
>                 if P>0 ->
>                         N * power(N,P - 1);
>                 true ->
>                         1 / power(N,-1 * P).
>
>
> Erlang (BEAM) emulator version 5.6.2 [async-threads:0]
>
> Eshell V5.6.2  (abort with ^G)
> 1> c(math).
> ./math.erl:20: syntax error before: '.'
> ./math.erl:2: function power/2 undefined
> error
> 2>
>
>
> am i using the "true ->" correctly? it is like else right?
>
> ------------------------------
> Sök efter kärleken!
> Hitta din tvillingsjäl på Yahoo! Dejting: http://se.meetic.yahoo.net<http://ad.doubleclick.net/clk;185753627;24584539;x?http://se.meetic.yahoo.net/index.php?mtcmk=148783>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080617/15397ae1/attachment.htm>


More information about the erlang-questions mailing list