需要帮助将 prop 写入 blak :: 数独 → [位置](哈斯克尔)

发布于 2024-10-05 16:19:37 字数 1693 浏览 0 评论 0原文

我偶然发现了这个线程 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不…忘初心 2024-10-12 16:19:37

我不确定您到底需要什么,所以我会告诉您编译器错误的含义。

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

prop_blank_pos :: Sudoku -> Bool
prop_blank_pos sud = all (==(Nothing, Nothing)) (blanks sud)

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 and pair !! 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 of Pos.


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 returns True if all elements of the_list are equal to (Nothing, Nothing).

prop_blank_pos :: Sudoku -> Bool
prop_blank_pos sud = all (==(Nothing, Nothing)) (blanks sud)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文