Breaking a list and tagging each element with an index number
How does one split a list which is passed as an argumen开发者_如何转开发t to a function and tag each element with a number?
The problem I have is how to increment in erlang as there are no for loops.
Thanks
Is this what you're trying to do?
tagger(List) ->
tagger(List, 0).
tagger([Head|Tail], Index) ->
[{Head, Index}|tagger(Tail, Index + 1)];
tagger([], _Index) ->
[].
Because if it is, you can use lists:mapfoldl
:
lists:mapfoldl(fun (A, AccIn) -> {{A, AccIn}, AccIn + 1} end, 0, List).
精彩评论