[erlang-questions] re moving nth element from a list
Wed Jun 25 13:20:00 CEST 2008
Paulo Sérgio Almeida <> wrote:
> This is one of those cases where it is not necessarily better to use
> tail calls; one must measure what happens. Here are two versions:
| > Body recursive:
| > remove(_, ) -> ;
| > remove(1, [_|T]) -> T;
| > remove(N, [H|T]) -> [H | remove(N-1, T)].
this looks pretty nice ! thanks.
| > Tail recursive:
| > remove2(N, L) -> remove2(N, L, ).
| > remove2(_, , Acc) -> lists:reverse(Acc);
| > remove2(1, [_|T], Acc) -> lists:reverse(Acc, T);
| > remove2(N, [H|T], Acc) -> remove2(N-1, T, [H|Acc]).
| > The second one seems to be better for removing near the end of a large
| > list, due to lists:reverse being builtin.
More information about the erlang-questions