我想在sml中仅使用更高阶函数来进行矩阵的转置,但我真的是堆栈,这就是我所拥有的
fun transpose [] = []
| transpose ([]::_) = []
| transpose mat = (map hd mat)::(transpose(map tl mat));
但我不想调用转置,因为再次不允许递归。我也想摆脱案件。任何帮助?
答案 0 :(得分:4)
transpose mat = tabulate (length (nth (mat,0)),fn i => map (fn l => (nth (l,i))) mat)
或者,为了提高空矩阵的可读性和安全性:
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