我刚开始学习编写 Haskell 代码,如果这是一个愚蠢的问题,我深表歉意。我试图通过使用[]
单子来重做八皇后问题。这是代码,
import Control.Monad
addqueen :: [Int] -> [[Int]]
addqueen xs = [ x:xs | x <- [1,2..8],
not $ x `elem` xs
|| (any (\ (index,q) -> abs (x-q) == index) $ zip [1..] xs) ]
当我尝试
[[]] >>= replicateM 8 addqueen
它不起作用,但会产生以下错误:
Couldn't match expected type `t0 -> t1' with actual type `[[a0]]'
The first argument of ($) takes one argument,
but its type `[[a0]]' has none
In the expression: [[]] >>= replicateM 8 $ addqueen
In an equation for `it': it = [[]] >>= replicateM 8 $ addqueen
那么我如何在这里实现我想要做的呢?