Strange, I tried looking for similar code in other languages (even other functional stuff) but it seems Lisp simply lends itself to this function.
I think this is due to the nature of LISP which uses the same structure (dynamically typed linked list) for data collections and function applications. This provides functions such as
apply and use the
mapcar with any number of lists.
With F#, which is statically typed, the implementation of a transpose function depends on the data structure used to represent the matrix.
Using a linked list as with LISP:
let m1 = [[1.; 2.]; [3.; 4.]; [5.; 6.]]
let rec transpose1 = function
| _ -> []
Which LISP equivalent is:
(setq m1 '
((1.
2.
) (3.
4.
) (5.
6.
)))
)
)
Using a 2 dimensional array (as in the link you posted)
let m2 = array2D [[1.; 2.]; [3.; 4.]; [5.; 6.]]
let transpose2 m =
Or simply using the Microsoft.FSharp.Math.Matrix<float> type (from the PowerPack library) which provides a Transpose property
let m3 = matrix [[1.; 2.]; [3.; 4.]; [5.; 6.]]
m3.Transpose