Created
August 30, 2012 05:13
-
-
Save l1x/3522630 to your computer and use it in GitHub Desktop.
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
(ns spiral) | |
(defmacro dbg[x] `(let [x# ~x] (println "dbg:" '~x "=" x#) x#)) | |
(def matrix1 [[1 2 3][8 9 4][7 6 5]]) | |
(def matrix2 [[1 2 3 4][12 13 14 5][11 16 15 6][10 9 8 7]]) | |
(defn spiral-print [matrix result] | |
(let [[row & rows] (seq matrix)] | |
(if (seq rows) | |
(recur | |
(reverse (apply map vector rows)) | |
;returns a new list with result + row | |
(into (dbg result) (dbg row))) | |
;else | |
result))) | |
;(->> [[8 9 4] [7 6 5]] (apply map vector)) | |
;([8 7] [9 6] [4 5]) | |
(spiral-print matrix1 []) | |
(spiral-print matrix2 []) |
Author
l1x
commented
Aug 30, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment