Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Transpose a matrix in Clojure
(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))))))
@bdesham

This comment has been minimized.

Copy link
Owner Author

bdesham commented Jul 6, 2011

Or even this:

(defn square-matrix?
  [mat]
  (apply =
         (count mat)
         (for [r mat] (count r))))

(defn transpose
  [mat]
  {:pre [(square-matrix? mat)]}
  (apply map vector mat))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.