Skip to content

Instantly share code, notes, and snippets.

@gdejohn
Created April 11, 2017 16:22
Show Gist options
  • Save gdejohn/6649695ea2083ac324156e1c2b9c5c7f to your computer and use it in GitHub Desktop.
Save gdejohn/6649695ea2083ac324156e1c2b9c5c7f to your computer and use it in GitHub Desktop.
Enumerate solutions to the n queens problem
import Control.Applicative (liftA2)
import Data.List (inits, permutations)
queens :: Int -> [[(Int, Int)]]
queens n = filter p $ liftA2 map zip permutations $ take n [0 ..]
where p x = and $ zipWith f x $ inits x
f (a, b) = all $ \(c, d) -> abs (a - c) /= abs (b - d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment