Created
July 6, 2011 16:50
-
-
Save bdesham/1067736 to your computer and use it in GitHub Desktop.
Transpose a matrix in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn square-matrix? | |
[mat] | |
(apply = | |
(count mat) | |
(for [r mat] (count r)))) | |
(defn transpose | |
[mat] | |
{:pre [(square-matrix? mat)]} | |
(loop [res [], | |
mat mat] | |
(if (empty? (first mat)) | |
res | |
(recur (conj res | |
(vec (for [r mat] (first r)))) | |
(for [r mat] (rest r)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or even this: