Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an object's numerical position in a list

Tags:

list

haskell

Is there any way to determine an object's numerical position in a list? That is in the list [1, 2, 3, 4] 1 would have the position of 0 and in the list "Hello, World!" l would have the position of 2? I tried to make a counter that bumped up while it was reading the list, but forgot Haskell doesn't allow variables to change. Any help is appreciated and thank you for reading

like image 648
D347th Avatar asked Mar 22 '26 20:03

D347th


1 Answers

Try Hoogle for this variety of question. For example, I just tried the (wrong, it turns out) type signature [a] -> a -> Int there, and the top two results were

elemIndex :: Eq a => a -> [a] -> Maybe Int
elemIndices :: Eq a => a -> [a] -> [Int]

which do just what you ask. Take a look at their source if you're curious how.

like image 116
Daniel Wagner Avatar answered Mar 25 '26 11:03

Daniel Wagner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!