Skip to content

Instantly share code, notes, and snippets.

@risou
Created Jan 1, 2014
Embed
What would you like to do?
99 questions No.90
generate :: Int -> [[Int]]
generate 0 = [[]]
generate n = iter [1..n] [] where
iter [] ys = return (reverse ys)
iter xs ys = do
x <- xs
iter (filter (/=x) xs) (x:ys)
queens :: Int -> [[Int]]
queens n = filter test $ generate n
test :: [Int] -> Bool
test [] = True
test (i:is) = not (check 1 i is) && test is
check :: Int -> Int -> [Int] -> Bool
check _ _ [] = False
check n j (i:is) =
if j == i + n || j == i - n
then True
else check (n+1) j is
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment