Tail/Non tail recursion

Joachim Durchholz <>
Thu Aug 28 16:03:59 CEST 2003


Jilani Khaldi wrote:
> 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([]) -> [].

Make it a one-liner using map(), like this:

	square(L) ->  map(fun (X) -> X*X end, L)

This is potentially more efficient than explicitly writing down the
recursion :-)

Regards,
Jo




More information about the erlang-questions mailing list