(def a '((1 2 3 4) (11 12 13 14) (21 22 23 24) (31 32 33 34)))
(defn tilt [l m r]
(if (and (empty? l) (empty? m))
r
(let [fl (first l)
nm (filter not-empty (map rest m))
nr (map first m) ]
(recur (rest l)
(if (empty? l) nm (cons (rest fl) nm))
(cons (if (empty? l) nr (cons (first fl) nr)) r) ))))
(println (reverse (tilt a () ())))
;; -> ((1) (11 2) (21 12 3) (31 22 13 4) (32 23 14) (33 24) (34))
2011/3/19 izy00466 <izy0...@gmail.com>:
--
Masakazu Takahashi (emasaka)
久々にClojureで書いてみました(汗)
リストの破壊的操作をしたかったのですが、Clojureだと汚くなっちゃいますね、、
(defn rearrange [m]
(map deref
(loop [n 0 ret '()]
(if (>= n (count zm))
ret
(recur (inc n)
(concat (take n ret)
(keep-indexed (fn [i e]
(if (> (count ret) (+ n i))
(ref
(let [r (nth ret (+ n i))]
(dosync
(ref-set r (concat @r
(list e))))))
(ref (list e))))
(nth m n))))))))
(defn clockwise-rotate "時計回りに45度回転" [m]
(rearrange (apply map list m)))
(defn counterclockwise-rotate "逆時計回りに45度回転" [m]
(rearrange (map reverse m)))
もとのリストを
(1 11 21 31)
(2 12 22 32)
(3 13 23 33)
(4 14 24 34)
にしてから
一行ずつ
1
11
21
31
↓
1
11 2
21 12
31 22
32
↓
1
11 2
21 12 3
31 22 13
32 23
33
↓
…
としたかっただけです。。
2011年3月19日16:19 izy00466 <izy0...@gmail.com>:
--
#|--------------------------------------
Toshiaki Maki
E-mail: ma...@ik.am
URL: http://blog.ik.am
----------------------------------------|#
2次元配列の斜め方向の検索という意味で考えると何かポピュラーな方法がありそうな気がして質問を出したのですが、やはりゴリゴリと並べ替えるというの
が一般的な考え方ということでよいのでしょうか?