eight queens

Eight queens puzzle, first attempt (will be updated later)


import Control.Monad

legalqueens :: [Int] -> Bool
legalqueens [] = True
legalqueens (s:sx) = not (or [not (legalqueens sx),
 s `elem` sx,
 s `elem` zipWith (-) sx [1..],
 s `elem` zipWith (+) sx [1..]
 ])
f1 = do
 a <- [1..8]
 b <- [1..8]
 c <- [1..8]
 d <- [1..8]
 e <- [1..8]
 f <- [1..8]
 g <- [1..8]
 h <- [1..8]
 guard $ legalqueens [a,b,c,d,e,f,g,h]
 return [a,b,c,d,e,f,g,h]

Advertisements

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: