A DSP task in Erlang ?

Massimo Cesaro massimo.cesaro@REDACTED
Tue Jan 20 17:26:14 CET 2004


On Tue, 2004-01-20 at 17:27, Fredrik Linder wrote:
> Well, lets take it one step further :-)
> 
> one part that sums all heads + gives all tails in a list of lists
> one part that traverses the list of lists
> 
> func([[A | B] | T]) ->
>     case func(T) of
>         {C, D} -> {A + C, [B | D]};
>     end;
> func([]) ->
>     {0, []}.
> 
> sum(T = [[] | ]) ->
>     %% Assume *all* lists are empty (you might like a more complete check...)
>     [];
> sum(T) ->
>     {A, B} = func(T),
>     [A | sum(B)].
> 
> sum([[1,2,3],[2,3,4]]) -> [3,5,7]. (unless I'm mistaken)
> 
> you might want to try it tail-recursive as well, but I'm not sure which is faster anymore.
> 
> Cheers
> /Fredrik
Looks pretty cool!
By modifying it slightly I can also manage the case when lists are of
different lengths (and then avoid the padding with silence).
I'll put this under test right now.
Thanks a lot!
Massimo





More information about the erlang-questions mailing list