Access to the Haskell Array (Matrix)

I am currently working on a project that will need to access the elements in the Matrix array in Haskell. So, I tried to search it all over the world.

The function is supposed to be like this:

getElementIndex :: Int -> Array (Int,Int) Int -> (Int,Int)

And it should return the indices Iand the Jelement in the matrix.

+3
source share
3 answers

To read elements from Arraytypes 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 you only need to walk around the index space, rows and columns. I like the understanding of lists for this kind of task:

assocs' x y arr = [ ((i,j), arr ! (i,j))
                  | i <- [0..x-1]
                  , j <- [0..y-1]
                  ]

which is only a specialized version Data.Array.assocs:

assocs :: Ix i => Array i e -> [(i, e)]

. , assocs, , .

+6

\x -> map fst . filter ((==x) . snd) . assocs
+4

All Articles