basically having this:
[
[1;2;3];
[4;5;7];
[8;9;0];
]
I would like to get this (read vertically/ turn 90 degrees):
[
[1;4;8];
[
What you need is called matrix transposition.
The simplest way is using FSharp.PowerPack; Microsoft.FSharp.Math.Matrix
module has Transpose
method.
If you prefer your own solution, here's the one that demonstrates a good combination of short code and executing efficiency:
let rec transpose = function
| (_::_)::_ as M -> List.map List.head M :: transpose (List.map List.tail M)
| _ -> []
// use
[[1; 2; 3]; [4; 5; 6]; [7; 8; 9]]
|> transpose
|> printfn "%A"
Yet another approach is in-place matrix transposition. It has complexity of O(n), but requires mutable data.
I would do it by converting to arrays -
let arr = input |> List.map (List.toArray) |> List.toArray //a array of arrays
let out = Array2D.create size1 size2 (fun x y -> arr.[y].[x])