Skip to content

Instantly share code, notes, and snippets.

@l1x
Created August 30, 2012 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l1x/3522630 to your computer and use it in GitHub Desktop.
Save l1x/3522630 to your computer and use it in GitHub Desktop.
(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 [])
@l1x
Copy link
Author

l1x commented Aug 30, 2012

spiral=>  (spiral-print matrix2 [])
dbg: result = []
dbg: row = [1 2 3 4]
dbg: result = [1 2 3 4]
dbg: row = [5 6 7]
dbg: result = [1 2 3 4 5 6 7]
dbg: row = [8 9 10]
dbg: result = [1 2 3 4 5 6 7 8 9 10]
dbg: row = [11 12]
dbg: result = [1 2 3 4 5 6 7 8 9 10 11 12]
dbg: row = [13 14]
dbg: result = [1 2 3 4 5 6 7 8 9 10 11 12 13 14]
dbg: row = [15]
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment