在clojure中,矩阵的可能表示是向量的向量,即[[1 2] [3 4]].转置矩阵的可能实现方式是:
(defn transpose [matrix]
(loop [matrix matrix, transp [], i 0]
(if (< i (count (nth matrix 0)))
(recur matrix
(conj transp
(vec (reduce concat
(map #(conj [] (nth %1 i))
matrix))))
(inc i))
transp)))
Run Code Online (Sandbox Code Playgroud)
任何人都可以想到更具惯用性的clojure实现吗?例如,以避免这个可怕的循环复发?