F # 2D array alternate creation

I am using Array2D module in f # and want to create its sudoku playfield (9x9 array) using it. Right now I have something that works and it looks like this:

let createInitialArray = [| [|for x in 1 .. 9 -> createSquare x 1 |]; 
                            [|for x in 1 .. 9 -> createSquare x 2 |]; 
                            [|for x in 1 .. 9 -> createSquare x 3 |]; 
                            [|for x in 1 .. 9 -> createSquare x 4 |]; 
                            [|for x in 1 .. 9 -> createSquare x 5 |]; 
                            [|for x in 1 .. 9 -> createSquare x 6 |]; 
                            [|for x in 1 .. 9 -> createSquare x 7 |]; 
                            [|for x in 1 .. 9 -> createSquare x 8 |]; 
                            [|for x in 1 .. 9 -> createSquare x 9 |] |]

let sudokuGame = Array2D.init 9 9 ( fun i j -> createInitialArray.[j].[i] )

      

My question is, is there a better or more compact way to write this?

On MSDN about arrays in general and MSDN about Array2D I found out that there are a few more functions like init, initBased, create and createBased. Since I have only been studying the language for a few more weeks, I do not see how I can work with them.

+3


source to share


1 answer


You can drop completely createInitialArray

and insert the call createSquare

:

let sudokuGame = Array2D.init 9 9 ( fun i j -> createSquare i j )

      



Or even shorter, throwing in a tautological lambda:

let sudokuGame = Array2D.init 9 9 createSquare

      

+4


source







All Articles