haskell - 需要帮助将 prop 写入 blak::Sudoku → [Pos] (Haskell)

标签 haskell ghci

我偶然发现了这个线程 Haskell List Comprehension现在我正在尝试为其编写一个 Prop ,指出该函数中的所有单元格实际上都是空白的,但在尝试编译它时仅得到了以下错误消息。

{-
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)”的第一个参数中

编辑

我的问题是,我从空白中得到的列表是一个包含[(Nothing,Nothing),(Nothing,Nothing)...etc]的列表[Pos]。

我想检查所有元组的两个元素实际上都是“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 )

最佳答案

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

fst 对元组 (a, b) 进行操作,但您给它一个 [Pos]

要么确保pair返回一个元组,要么使用列表函数来获取第一个和第二个元素,例如第一对头对对!! 1 表示第二个元素。

在我看来,您希望 pair 返回一个元组,但这并没有真正发生。 blanks sud 返回一个 Pos 列表。


编辑:好的,所以 Pos 是一个元组,您想检查 [Pos] 是否仅包含等于 (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)

关于haskell - 需要帮助将 prop 写入 blak::Sudoku → [Pos] (Haskell),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4302900/

相关文章:

haskell - 让函数根据参数的值返回不同的类型

haskell - 数据声明的类型类约束

haskell - 在 Haskell 中使用自己的类型类实现最大迭代器时无法遵循类型

parsing - haskell 中的暂停点何时应该与额外的 "space"一起使用?

haskell - 在haskell中具有多值函数的函数组合?

haskell - GHCi 是否不应用默认声明来解决类型歧义?

haskell - 在树莓派上编译 Haskell

haskell - 具有函数组合的类型推断列表

haskell - 如何在 ghci 中正确使用 PackageImports?

haskell - 在 Nat 上使用 * 作为原语