Giving an element in a list, which function can I use to find its index?
For instance, I want to find the index of 3 in the list [1, 2, 3, 4]
.
Which function exists in Haskell that I can use for this?
To find the index of an element in a list, you use the index() function. It returns 3 as expected. However, if you attempt to find an element that doesn't exist in the list using the index() function, you'll get an error.
The indexOf(Object) method of the java. util. ArrayList class returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. Using this method, you can find the index of a given element.
How to Find the Index of a List Element in Python. You can use the index() method to find the index of the first element that matches with a given search object. The index() method returns the first occurrence of an element in the list. In the above example, it returns 1, as the first occurrence of “Bob” is at index 1.
Take a look here:
i.e. use elemIndex
from Data.List
:
elemIndex :: Eq a => a -> [a] -> Maybe Int
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With