; unwrapping a spiral
(define (matrix-rows x) (vector-length x))
(define (matrix-cols x) (vector-length (vector-ref x 0)))
(define (matrix-ref m i j) (vector-ref (vector-ref m i) j))
(define (spiral m)
(let ((r (matrix-rows m)) (c (matrix-cols m)))
(let loop ((x -1) (y 0) (dx 1) (dy 0) (r r) (c c) (i 0) (xs '()))
(cond ((zero? r) (reverse xs))
((= i c) (loop x y (- dy) dx c (- r 1) 0 xs))
(else (let ((x (+ x dx)) (y (+ y dy)))
(loop x y dx dy r c (+ i 1)
(cons (matrix-ref m y x) xs))))))))
(display (spiral #(
#( 1 2 3 4)
#( 5 6 7 8)
#( 9 10 11 12)
#(13 14 15 16)
#(17 18 19 20))))