If you want to collapse yourself, this seems to work as needed:
foreach_index(F, [H|T]) ->
foreach_index(F, [H|T], 0).
foreach_index(F, [H|T], N) ->
F(H, N),
foreach_index(F, T, N + 1);
foreach_index(F, [], N) when is_function(F, 2) -> ok.
The function Fwill be called with two parameters - a separate entry from the list and its index.
source
share