Jordan Mironski
Jordan Mironski

Reputation: 35

Finding the index of current element in a map function

I have a list with elements that can have duplicates and i want to

map a function for example: map (\x -> f (5 + (elemIndex x l))) l The problem is that if the list has duplicate elements elemIndex returns the index of the first one. I want it to be the index of x in l ,where x is the current element.

I guess I am looking for a function to replace elemIndex in my code

Upvotes: 1

Views: 202

Answers (1)

Li-yao Xia
Li-yao Xia

Reputation: 33464

You can zip your list with a list of indices before mapping. There also is a shorthand zipWith for zip followed by map.

zipWith (\i x -> f (5 + i)) [0 ..] l

Upvotes: 5

Related Questions