nQueens = {
const candidates = [];
function verifyBoard (board) {
candidates.push(board);
return tails(board).every(
([x, ...xs]) => xs.every(
(y, i) => x !== y && Math.abs(x - y) !== i + 1
)
)
}
const elementSelect = domain => new Select(epsilon(domain));
const sequenceSelect = domain => new Select(k => {
if (!domain.length || !k([])) {
return [];
}
const s = elementSelect(domain).chain(
choice => R.map(
R.prepend(choice),
sequenceSelect(domain.filter(x => x !== choice))
)
);
return runSelect(s)(k);
});
const testFunc = R.memoizeWith(R.identity, verifyBoard);
const solution = runSelect(sequenceSelect(R.range(0, n)))(testFunc);
return { candidates, solution };
}