How would I use lens in Haskell to duplicate Python's enumerate?
问题 Python's enumerate on lists can be written as zip [0..] . I looked at Control.Lens.Traversal and Control.Lens.Indexed, but I couldn't figure out how to use lenses to generalize this to any reasonable container (I hesitate to say "Traversable"). I'm guessing itraverse or itraverseOf is key. 回答1: If you're using a container that is an instance of FunctorWithIndex then you can simply use imap (,) : > imap (,) "abc" [(0,'a'),(1,'b'),(2,'c')] But if the index isn't the position this won't work: >