3

I am using the Array2D module in f# and want to create a sudoku game board (a 9x9 array) using it. Right now I have something that works and that 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 if there is a better or more compact way to write this?

In MSDN about arrays in general and MSDN about Array2D I've learned that there are a few other functions, for example init, initBased, create and createBased. As I'm still only a few weeks into learning the language I don't see how I can work with them.

1 Answers1

4

You can totally do away with createInitialArray and inline the call to createSquare:

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

Or even shorter, dropping the tautological lambda:

let sudokuGame = Array2D.init 9 9 createSquare
Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172