1

I currently have a 2D array of the form UArray (Int, Int) Char, I would like to turn this into a List like so:

["ffffffffff",
"ffffffffff",
"ffffffffff",
"ffffffffff",
"ffffffffff",
"ffffffffff",
"ffffffffff",
"ffffffffff",
"ffffffffff",
"ffffffffff"]

Each f is originally a character in the UArray.

Where the Chars for each line in the 2D array is turned into a String, which, in turn is parsed into a List as such [String], however, I have had no luck with the toList method from Data.Foldable, what functions should I be looking at to achieve this?

Thanks for any help!

duplode
  • 33,731
  • 7
  • 79
  • 150
  • I have updated the question I asked prior to this so it is no longer a repeat, the previous question did not go over flattening in a manner which worked for my code. –  Oct 13 '15 at 20:10

1 Answers1

0

You can generalize this functionality quite a bit, for any tuple of indices, any array type and any element type:

import Data.Array.Unboxed 

flatten2D :: (Enum i0, Enum i1, Ix i0, Ix i1, IArray arr x) 
          => arr (i0, i1) x -> [[x]]
flatten2D arr = [ [ arr ! (i0, i1) | i0 <- [min0..max0] ] | i1 <- [min1..max1] ]
  where ((min0, min1), (max0, max1)) = bounds arr 

The implementation is straightforward: the indices lists are constructed using the Enum instance from a minimum and maximum bound. Then you simply get the element at each index. A nested list comprehension accomplishes this in a very nice way.

user2407038
  • 14,400
  • 3
  • 29
  • 42