简体   繁体   English

Haskell:调用列表中每个元素的函数

[英]Haskell: Call a function with each element in a list

I am making a sudoku solving program and I have a potentialNbrsAt function that gets the numbers that could be at position x y. 我正在制作一个数独解决程序,并且我有一个potentialNbrsAt函数,该函数获取可能位于位置x y的数字。

Now, I am trying to get the intersect of each lists of potential numbers in a column. 现在,我试图在列中获得每个潜在数字列表的交集。 Something like the onlyOnePlaceForNbrInCol function bellow. 类似于onlyOnePlaceForNbrInCol函数。

Code: 码:

potentialNbrsAt :: Int -> Int -> Sudoku -> [Int]
potentialNbrsAt x y sudoku = intersect rowMissingNbrs $ intersect colMissingNbrs sqrMissingNbrs
                where rowMissingNbrs = getMissingNbrs $ getRow y sudoku
                      colMissingNbrs = getMissingNbrs $ getCol x sudoku
                      sqrMissingNbrs = getMissingNbrs $ getSquare squareIndex sudoku
                      squareIndex    = 3 * (y `div` 3) + (x `div` 3)

onlyOnePlaceForNbrInCol :: Int -> Int -> Sudoku -> Bool
onlyOnePlaceForNbrInCol colIndex nbr sudoku = -- What goes here? Some pointers please???

I think onlyOnePlaceForNbrInCol should, at some point, call potentialNbrsAt with each numbers from 0 to 8 as an argument for y. 我认为,在某个时候,只有OnePlaceForNbrInCol应该调用potentialNbrsAt,其中每个数字从0到8作为y的参数。 Telling me how to do this would greatly help. 告诉我该怎么做将大有帮助。

因此,您试图确定是否所有数字[0..8]满足给定的谓词。

What about [ potentialNbrsAt xy sudoku | y <- [0..8] ] 怎么样[ potentialNbrsAt xy sudoku | y <- [0..8] ] [ potentialNbrsAt xy sudoku | y <- [0..8] ] ? [ potentialNbrsAt xy sudoku | y <- [0..8] ]吗? This gives you a list of all the results for such values of y . 这为您提供了y值的所有结果的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM