Skip to content

Instantly share code, notes, and snippets.

@risou
Created January 1, 2014 10:05
Show Gist options
  • Save risou/8206613 to your computer and use it in GitHub Desktop.
Save risou/8206613 to your computer and use it in GitHub Desktop.
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