Tail/Non tail recursion

Vlad Dumitrescu vlad_dumitrescu@REDACTED
Thu Aug 28 14:36:12 CEST 2003


Hi,

> Is there a better way to write this code when there is a lot of elements in 
> the list?
> 
> square([H|T]) -> [(H*H|square(T)];
> square([]) -> [].

For example,
 
square(L) ->
    square(L, []).

square([], Res) -> 
    lists:reverse(Res).
square([H|T], Res) -> 
    square(T, [H*H|Res]);

regards,
Vlad



More information about the erlang-questions mailing list