Reputation: 287
In F#, how would I make the list transposition function compatible with an int list list list ?
let rec transpose =
function
| (_::_)::_ as M -> List.map List.head M :: transpose (List.map List.tail M)
| _ -> []
It looks like this only works with int list list
. However, I have further grouped the inner most list to make int list list list
.
Upvotes: 0
Views: 501
Reputation: 2493
With an int list list list, it depends on which matrices you want to transpose: the inner ones or the outer one or both. In the last case it will depend on the transpositions order.
let rec transpose = function
| (_::_)::_ as M -> List.map List.head M :: transpose (List.map List.tail M)
| _ -> []
// (a (b (c))) -> (a (c (b)))
let transpose0 m = m |> List.map transpose
// (a (b (c))) -> (b (a (c)))
let transpose1 m = m |> transpose
// (a (b (c))) -> (b (c (a)))
let transpose2 m = m |> transpose |> List.map transpose
// (a (b (c))) -> (c (a (b)))
let transpose3 m = m |> List.map transpose |> transpose
// (a (b (c))) -> (c (b (a)))
let transpose4 m = m |> transpose |> List.map transpose |> transpose
Upvotes: 2