Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(!!) operator with overflowing integer

Tags:

haskell

I have just recently started learning Haskell and started playing around with the idea of infinite lists and lazy evaluation. I constructed an infinite list and tried to access an element at a very, very distant index using the !! operator. The issue is that the type signature for the !! operator is the following:

(!!) :: [a] -> Int -> a

Which means it takes an Int as an index to retrieve that element from the list.

Now, my problem occurs whenever I try to index something so distant that it overflow the Int and therefore goes negative. What would then be the proper Haskell way of doing this?

like image 732
Alexander Ventura Avatar asked Jan 12 '23 08:01

Alexander Ventura


1 Answers

Data.List.genericIndex supports using any Integral number to index, so you can use Integer if you want.

like image 160
amalloy Avatar answered Jan 13 '23 22:01

amalloy