F # convert Array2 to list

I am still new to functional programming, so if I cannot figure out how to do something, I will return to the procedural style. I found a way around to convert to a list, but I still would like to know how to do this.

Here is my attempt to convert a two-dimensional array to a list.

let board = Array2.init 10 20 (fun ij -> pull(i, j)) let mutable pieces = [] board |> Array2.mapi (fun ija -> transform(i, j, a)) |> Array2.iter (fun a -> (pieces <- a :: pieces)) 
+4
source share
1 answer

Apparently, in .Net, multidimensional arrays are IEnumerable (not shared), and so this works:

 let a2 = Array2.init 2 3 (fun xy -> (x+1)*(y+1)) let l = a2 |> Seq.cast<int> |> Seq.fold (fun ln -> n :: l) [] printfn "%A" l 

EDIT: As Noldorin notes in a comment, this is even better:

 let l = a2 |> Seq.cast<int> |> Seq.toList 
+7
source

All Articles