Links und Funktionen
Sprachumschaltung

Navigationspfad
Sie sind hier: Startseite / Lehre / SS 2012 / Funktionale Programmierung / Folien / 8QueensV4


Inhaltsbereich

8QueensV4

Vierte Version von EightQueens.hs

Haskell source code icon EightQueensBoardCheckEmptyCol.hs — Haskell source code, 1 KB

Dateiinhalt

{-# LANGUAGE ScopedTypeVariables #-}
module Main where

import System (getArgs)

-- | Produce all queen positionings on a board of size @n*n@.
queens n = choose (board n)

-- | Initially, for each row, all possible columns are unthreatened.
board n = take n (repeat [1..n])

-- | List the valid queen positionings for a row list of unthreathened column positions.
choose []     = [[]]
choose rs | any null rs = []
choose (r:rs) = [ c:cs | c  <- r   -- pick an available column
                       , cs <- choose (removeThreatened c rs) ]

-- | Delete column positions in remaining rows @rs@
--   threatened by a queen in column @c@ of the current row.
removeThreatened c rs = zipWith (del c) [1..] rs
  where del c n = filter (\ y -> let d = abs(c-y) in d /= 0 && d /= n)

main = do
  args <- getArgs
  let n :: Int = read $ head args
  putStrLn $ show $ map (length . queens) [1..n]

Artikelaktionen


Funktionsleiste