1

Is there a function in Haskell that takes as arguments a list and an element in that list and returns the index of that element in the list, i.e.

If I had the list ['a','f','d','g','b','h'] and the element 'b' it would return 4?

2

1 Answer 1

3

You're looking for the elemIndex function in Data.List:

> :m +Data.List
> :t elemIndex
elemIndex :: Eq a => a -> [a] -> Maybe Int
> elemIndex 'c' "abcde"
Just 2
Sign up to request clarification or add additional context in comments.

1 Comment

I think that worth teaching the fromJust function, too. :) More references at Data.Maybe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.