NQueen in Haskell
Posted on September 2, 2022
Tags: algorithms
8 queens
type Row = Int
type Col = Int
type Coord = (Row, Col)
type Board = [Row]
queens :: Col -> [Board]
0 = [[]]
queens | n > 0 = [q :qs | q <- [1..8],
queens n <- queens (n-1),
qs and [not (check (1,q) (x,y)) | (x,y) <- zip [2..n] qs]]
check :: Coord -> Coord -> Bool
= x == x' || y == y' || x + y == x' + y' || x-y == x'-y' check (x,y) (x',y')