[erlang-questions] How to write this code better?

Yuri Ginsburg <>
Sat Apr 12 13:36:25 CEST 2008


I'd probably white a separate function for  the sum Fourier series, just
for  the  case if I have to calculate another series.
Something like this:

fourier(A, B, X) ->
    fourier(A, B, X, 1, 0).

fourier([], [], _,  _, Acc) ->
    Acc;

fourier([H1|T1], [H2|T2], X, Pos, Acc) ->
    Arg = Pos * X,
    fourier(T1, T2, X, Pos+1, Acc + H1 * math:cos(Arg) + H2 *
math:sin(Arg)).

--yg

2008/4/11, Jilani Khaldi <>:
>
> Hi All,
> I am translating this Pascal code:
>
>      t := 2*3.14159*((JD - 1) / 365.0);
>      declination := (0.322003 - 22.971 * cos(t)
>                   - 0.357898 * cos(2*t)
>                   - 0.14398 * cos(3*t)
>                   + 3.94638 * sin(t)
>                   + 0.019334 * sin(2*t)
>                   + 0.05928 * sin(3*t));
>
>         // Check it
>         if (declination > 89.99) then declination := 89.99;
>         if (declination < -89.99) then declination := -89.99;
>
>   to Erlang:
>
>    T1 = 2*3.14159*((JD - 1) / 365.0),
>    Declination1 = (0.322003-22.971*cos(T1)
>     -0.357898*cos(2*T1)
>     -0.14398*cos(3*T1)
>     +3.94638*sin(T1)
>     +0.019334*sin(2*T1)
>     +0.05928*sin(3*T1)),
>
>    % Check it
>    if (Declination1 > 89.99) ->
>       Declination2 = 89.99;
>    true ->
>      Declination2 = Declination1
>    end,
>    if (Declination1 < -89.99) ->
>       Declination2 = -89.99;
>    true ->
>      Declination2 = Declination1
>    end,
>    Declination2.
>
> The Erlang code works but I think there is at least one way to write it
> better.
> Thank you!
>
> --
> ***
> Jilani KHALDI
> http://www.dotpas.org
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080412/b81d6267/attachment.html>


More information about the erlang-questions mailing list