Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the index of an element in a list?

Tags:

haskell

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?

like image 430
Kap Avatar asked Nov 25 '10 00:11

Kap


People also ask

How do you find the index value of an element in a list?

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.

How do you find the index of an element in a list in Java?

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 do I find the index of a character in a list?

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.


1 Answers

Take a look here:

  • Finding index of element in a list in Haskell?

i.e. use elemIndex from Data.List:

  • http://hackage.haskell.org/packages/archive/base/4.2.0.0/doc/html/Data-List.html#v%3AelemIndex
elemIndex :: Eq a => a -> [a] -> Maybe Int 
like image 180
icyrock.com Avatar answered Sep 28 '22 16:09

icyrock.com