0

I tried to get nth array from 2d array but I couldn't. For example, I need to get second array. {[1,2,3], [4,5,6], [7,8,9]}

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

How should I implement this function? Thanks in advance.

Voro
  • 235
  • 1
  • 12

1 Answers1

3

I am pretty sure you cannot define an array as {[1,2,3], [4,5,6], [7,8,9]}, at least if we're talking about Data.Array.

Lists

If you are talking about a list, then if you look on Hoogle for [a] -> int -> a, you'll see (!!) :: [a] -> Int -> a is the function you're looking for. If you just need the second one, you can have point free

getSecond :: [[Int]] -> [Int]
getSecond = (!!2)

Arrays

If you're talking about Data.Array, then with a similar Hoogle search you'll find there's a function for that as well: (!) :: Ix i => Array i e -> i -> e

your array should be defined as Array Int (Array Int Int), and your function would be

getSecond :: Array Int (Array Int Int) -> Array Int Int
getSecond = (!2)
Lorenzo
  • 2,160
  • 12
  • 29