やってて楽しいプログラミング言語は? 3言語
■ このスレッドは過去ログ倉庫に格納されています
0772デフォルトの名無しさん
2011/11/09(水) 19:04:51.92でももっと短く書けるに違いない
module Main(main) where
import Control.Monad
import Data.Tuple
main = print $ nQueen $ 8
nQueen n = nQueen_ (n-1) (map return [1..n])
where nQueen_ 0 result = result
nQueen_ c base = do xs <- base
x <- [1..n]
guard (cond x xs)
(nQueen_ (c-1)) $ return $ (x:xs)
cond x xs = let list = zip (iterate (subtract 1) (length (x:xs))) (x:xs)
(p:ps) = tUnion (+) list
(m:ms) = tUnion (-) list in
notElem x xs && notElem p ps && notElem m ms
tUnion f [] = []
tUnion f (x:xs) = (f (fst x) (snd x)):(tUnion f xs)
■ このスレッドは過去ログ倉庫に格納されています