In Python enumerate works as follows:
a_list = ['a', 'b', 'c']for i, x in enumerate(a_list): print(i, x)
The output will be:
0 a1 b2 c
Thus, enumerate actually returns a generator (pretty much a lazy sequence) of pairs of the form (i, x) where i ranges over 0, 1, 2, ... and x are the elements of the list in order.
So far I have come up with this definition for lists, which does not produce a "generator" but also a list of pairs:
let enumerate (a_list: 'a list): (int * 'a) list = let rec _enumerar (a_list: 'a list) (accum: (int * 'a) list) (i: int): (int * 'a) list = match a_list with | [] -> accum | x::xs -> _enumerar xs ((i, x)::accum) (i+1) in _enumerar a_list [] 0 |> List.rev
Example usage:
# enumerate ['a'; 'b'; 'c'];;- : (int * char) list = [(0, 'a'); (1, 'b'); (2, 'c')]
Any ideas whether this function perhaps with a different name is implemented anywhere in the standard library or in Base?
What about a lazy version using Sequence or Stream?