[ create a new paste ] login | about

Link: http://codepad.org/26yqRd2B    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Oct 14:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
; spiral wrapping

(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 (unwrap m)
  (let ((r (matrix-rows m)) (c (matrix-cols m)))
    (let loop ((x c) (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 (unwrap #(
  #( 1  2  3  4)
  #( 5  6  7  8)
  #( 9 10 11 12)
  #(13 14 15 16)
  #(17 18 19 20))))


Output:
1
(4 3 2 1 5 9 13 17 18 19 20 16 12 8 7 6 10 14 15 11)


Create a new paste based on this one


Comments: