Learning haskell and want a function to generate a 2D grid similar to how you might in C:
int data[3][3]
what’s an acceptable and elegant approach? Zip? Foldl?
I could declare one like:
x = [[0,0,0],
[0,0,0],
[0,0,0]]
But I would like a function with x y parameters. Struggling to understand the easiest way without for/while loops 🙁
You seem to be asking “what should I use instead of Arrays in Haskell”, right? You asked about using lists, which certainly aren’t arrays and should be avoided for any serious work requiring non-sequential access (for example, lists give O(n) element access instead of O(1)).
The packages you should consider: array (old, standard Haskell arrays), vector (new, uses stream fusion, fast, an API that’s actually reasonable, boxed or unboxed, but only one dimension unless you nest them), and repa (only works on newer GHC versions, but allows multi-dimensional arrays and parallel operations even on unboxed representations).
The easiest way to initialize any of these (I assume you mean “initialize” when you say “generate”) is their respective “fromList” functions. For example: