transpose mat = tabulate (length (nth (mat,0)),fn i => map (fn l => (nth (l,i))) mat)
Or, for more readability and safety on empty matrices:
transpose mat =
let
val rows = length mat
val cols = if rows > 0
then length (nth (mat,0))
else 0
in
tabulate (cols, fn i => map (fn row => (nth (row, i)) mat)
end