{- queens9.out, as produced by this program, is 7041 bytes -}
main = writeQueens 9
writeQueens n = writeFile fn (show (queens n))
where
fn = "queens" ++ show n ++ ".out"
queens b = queens' b
where
queens' 0 = [[]]
queens' n = [ p ++ [n] | p <- queens' (n - 1), n <- [1..b], safe p n ]
safe p n = and [ not (check (i, j) (m, n)) | (i, j) <- zip [1 .. l] p]
where l = length p; m = l + 1
check (i, j) (m, n) = j == n || i + j == m + n || i - j == m - n