Here's an implementation of the n queens algorithm in the wikipedia article:
(def rot ((x . y)) (join y `(,x)))
(def nqueens (n)
(withs (m (mod n 12) r (range 1 n) od (keep odd r) cod (cddr od) s '(1 3) cods (join cod s) ev (keep even r))
(join (if (pos m '(3 9)) (rot ev) ev)
(case m
8 (apply join (map rev (pair od)))
2 (join (rev s) (rot cod))
3 cods
9 cods
od))))
Arc makes it very nice to implement, although it still bugs me that you can't use join to add a single item to the end of a list (which I've wanted to do a few times already). My preferred semantics:
The challenge is to produce all 92 solutions as the Ruby code does in the OP. I linked to wikipedia for the description of the problem, not the algorithm.
I also showed the ellided output for clarification.
class Array
def rot
push( shift )
end
end
nqueens = proc{|n|
odds, evens = (1..n).partition{|x| x % 2 > 0 }
case n % 12
when 2
odds = [3,1] + odds[3..-1] + [5]
when 3, 9
odds.rot.rot
evens.rot
when 8
d = -2
odds.map!{|x| d *= -1; x + d}
end
p evens + odds
}
nqueens[8]
class Array
def rot
push( shift )
end
end
proc{|n| p ((1..n).partition{|x|x%2>0}.inject{|o,e|
e + case n % 12
when 2
[3,1]+o[3..-1]+[5]
when 3,9
o.rot.rot;e.rot;o
when 8
d=-2;o.map{|x| x + d*=-1} end})}[ 8 ]
Is nil! an Anarki thing? (wipe m) is the Arc expression to set m to nil. (And (assert m) sets m to t. Assert tops my list of confusingly named functions.)