开发者

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).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜