需要帮助将 prop 写入 blak :: 数独 → [位置](哈斯克尔)
我偶然发现了这个线程 Haskell 列表理解 现在我正在尝试为其编写一个道具,说明所有该函数中的单元格实际上是空白的,但在尝试编译它时仅得到了以下错误消息。
{-
Property that states that all cells in the blanks list are actually blank
-}
prop_blank_pos :: Sudoku → Bool
prop_blank_pos sud = (rows sud) !! (fst pair) !! (snd pair) ≡ Nothing
where pair = blanks sud
无法将预期类型“(a, b)”与推断类型“[Pos]”匹配 在第一个参数“fst”namley 对和“(!!)”namley fst 对的第二个参数中 在 '(rows)bankey ('rows sud)' 的第一个参数中
编辑
我的问题是,我从空白中得到的列表是一个列表 [Pos] 包含 [(Nothing,Nothing),(Nothing,Nothing)... ETC]。
我想检查所有元组的两个元素实际上都是“Nothing”,即 [Pos] 中的所有元素都是(Nothing,Nothing)。我怎样才能检查这个,任何人都可以写一个代码示例,我不擅长 haskell 语法。
编辑 2
这是一个数独的例子
example :: Sudoku
example =
Sudoku
[ [Just 3, Just 6, Nothing,Nothing,Just 7, Just 1, Just 2, Nothing,Nothing]
, [Nothing,Just 5, Nothing,Nothing,Nothing,Nothing,Just 1, Just 8, Nothing]
, [Nothing,Nothing,Just 9, Just 2, Nothing,Just 4, Just 7, Nothing,Nothing]
, [Nothing,Nothing,Nothing,Nothing,Just 1, Just 3, Nothing,Just 2, Just 8]
, [Just 4, Nothing,Nothing,Just 5, Nothing,Just 2, Nothing,Nothing,Just 9]
, [Just 2, Just 7, Nothing,Just 4, Just 6, Nothing,Nothing,Nothing,Nothing]
, [Nothing,Nothing,Just 5, Just 3, Nothing,Just 8, Just 9, Nothing,Nothing]
, [Nothing,Just 8, Just 3, Nothing,Nothing,Nothing,Nothing,Just 6, Nothing]
, [Nothing,Nothing,Just 7, Just 6, Just 9, Nothing,Nothing,Just 4, Just 3]
]
编辑 3 这是数独的定义方式
data Sudoku = Sudoku { rows :: [[Maybe Int]] }
deriving ( Show, Eq )
i stumbled over this thread Haskell List Comprehension And am now trying to write a prop for it that states that all cells in this function actually are blank, but have only gotten this far with the following error message when trying to compile it.
{-
Property that states that all cells in the blanks list are actually blank
-}
prop_blank_pos :: Sudoku → Bool
prop_blank_pos sud = (rows sud) !! (fst pair) !! (snd pair) ≡ Nothing
where pair = blanks sud
could't match expected type '(a, b)' against inferred type '[Pos]'
in the first argument 'fst'' namley pair and second of '(!!)' namley fst pair
in the first argument of '(rows) bankey ('rows sud)'
Edit
My question is, the list that i get from blanks is a list [Pos] containing [(Nothing,Nothing),(Nothing,Nothing)...etc].
I want to check that all tuples both elements actually are "Nothing", i.e all Elements in the [Pos] are (Nothing,Nothing). How can I check this, Can anybody write a code sample, Im not good at the haskell syntax.
Edit 2
Here is an example of a soduku
example :: Sudoku
example =
Sudoku
[ [Just 3, Just 6, Nothing,Nothing,Just 7, Just 1, Just 2, Nothing,Nothing]
, [Nothing,Just 5, Nothing,Nothing,Nothing,Nothing,Just 1, Just 8, Nothing]
, [Nothing,Nothing,Just 9, Just 2, Nothing,Just 4, Just 7, Nothing,Nothing]
, [Nothing,Nothing,Nothing,Nothing,Just 1, Just 3, Nothing,Just 2, Just 8]
, [Just 4, Nothing,Nothing,Just 5, Nothing,Just 2, Nothing,Nothing,Just 9]
, [Just 2, Just 7, Nothing,Just 4, Just 6, Nothing,Nothing,Nothing,Nothing]
, [Nothing,Nothing,Just 5, Just 3, Nothing,Just 8, Just 9, Nothing,Nothing]
, [Nothing,Just 8, Just 3, Nothing,Nothing,Nothing,Nothing,Just 6, Nothing]
, [Nothing,Nothing,Just 7, Just 6, Just 9, Nothing,Nothing,Just 4, Just 3]
]
Edit 3
Here is how sudoku is defined
data Sudoku = Sudoku { rows :: [[Maybe Int]] }
deriving ( Show, Eq )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您到底需要什么,所以我会告诉您编译器错误的含义。
fst
对元组(a, b)
进行操作,但您给它一个[Pos]
。确保
pair
返回一个元组,或者使用列表函数来获取第一个和第二个元素,例如headpair
用于第一个和pair! 1
表示第二个元素。在我看来,您希望
pair
返回一个元组,但这并没有真正发生。blanks sud
返回一个Pos
列表。编辑:好的,所以
Pos
是一个元组,并且您想检查[Pos]
是否仅包含等于(Nothing, Nothing) 的元组
。正如戴夫在评论中所说,要做到这一点,你可以尝试像
all (==(Nothing, Nothing)) the_list
这样的东西。如果the_list
的所有元素都等于(Nothing, Nothing)
,则返回True
。I'm not sure what you need exactly, so I'll tell you what the compiler error means.
fst
operates on tuples(a, b)
, but you're giving it a[Pos]
.Either make sure
pair
returns a tuple, or use the list functions for fetching the first and second element, e.g.head pair
for the first andpair !! 1
for the second element.It seems to me you want
pair
to return a tuple, but that isn't really happening.blanks sud
is returning a list ofPos
.Edit: okay, so a
Pos
is a tuple, and you want to check if a[Pos]
contains only tuples which are equal to(Nothing, Nothing)
.As Dave said in the comments, to do this, you could try something like
all (==(Nothing, Nothing)) the_list
. This returnsTrue
if all elements ofthe_list
are equal to(Nothing, Nothing)
.