4 ms·
Actually, this is better (and no less clear): import Data.List queens n = q [] [0..n-1] where q s [] = [[]] q s as = concatMap (\a -> q (a:s) (delete
by user2994cb 10y ago
Actually, this is better (and no less clear):
import Data.List
queens n = q [] [0..n-1] where
q s [] = [[]]
q s as = concatMap (\a -> q (a:s) (delete a as)) (filter (f 1 s) as)
f _ [] a = True
f n (b:s) a = a /= b+n && a /= b-n && f (n+1) s a
main = print $ length (queens 8)
Undoubtedly functional, the question is whether it is improved eg. by using folds instead of explicit recursion, or replacing concatMap with monadic join.
- user2994cb 10y agoThat should be "q s [] = [s]", otherwise you get 92 copies of the empty list.