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]
queens 0 = [[]]
queens n | n > 0 = [q :qs | q <- [1..8],
                    qs <- queens (n-1),
                    and [not (check (1,q) (x,y)) | (x,y) <- zip [2..n] qs]]
check :: Coord -> Coord -> Bool
check (x,y) (x',y') = x == x' || y == y' || x + y == x' + y' || x-y == x'-y'