Need help writing prop to blak :: Sudoku → [Pos] (Haskell)

一曲冷凌霜 提交于 2019-12-11 03:07:23

问题


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 )

回答1:


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)


来源:https://stackoverflow.com/questions/4302900/need-help-writing-prop-to-blak-sudoku-%e2%86%92-pos-haskell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!