2 (:use [sicp.ch2-2 :only (enumerate-interval accumulate append flatmap)]))
4 (declare safe? empty-board adjoin-position)
6 (defn queens [board-size]
7 (let [f (fn queen-cols [k]
11 (fn [positions] (safe? k positions))
15 (adjoin-position new-row k rest-of-queens))
16 (enumerate-interval 1 board-size)))
17 (queen-cols (- k 1))))))]
22 (defn adjoin-position [row col positions]
23 (append positions (list (list row col))))
39 (defn same-diag? [p q]
40 (= (Math/abs (- (get-row p) (get-row q)))
41 (Math/abs (- (get-col p) (get-col q)))))
43 (defn under-attack? [p q]
48 (defn safe? [col positions]
49 (loop [this-pos (nth positions (- col 1))
50 rest-pos (remove #(= this-pos %) positions)]
53 (let [new-pos (first rest-pos)]
54 (if (under-attack? this-pos new-pos)
56 (recur this-pos (rest rest-pos)))))))