I'm currently in a project that will need to access elements in an Array-Matrix in Haskell. So, I've tried googling it, searching everywhere.
The funcion is supposed to be like this:
getElementIndex :: Int -> Array (Int,Int) Int -> (Int,Int)
And it must return the I and J indexes of the element in the matrix.
To read elements from Array types in Haskell, you use the (!) operator, as in:
Prelude Data.Array> let v = listArray (0,9) [1..10]
Prelude Data.Array> v ! 3
4
so, now all you need to do is walk the index space, rows and columns. I like list comprehensions for that kind of task:
assocs' x y arr = [ ((i,j), arr ! (i,j))
| i <- [0..x-1]
, j <- [0..y-1]
]
which is just a specialized version of Data.Array.assocs:
assocs :: Ix i => Array i e -> [(i, e)]
which returns a lazy list of indices and elements. So, call assocs, and then take the first element that matches.
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