简体   繁体   中英

How can I check that the length of a list of custom data types in Haskell?

I'm making a simple sudoku program that only utilises a 9 x 9 grid. To that end, I have a function to check it is 9 x 9 and also checks to make sure the inputted values are Just Num 's.

Here's the closest solution I've come to, I'm thinking the issue is in the pattern match I think (correct me if I'm wrong), this is because it compiles but has the logical error of returning False not True when given a perfectly fine test case. Anyways, here's the code dump :D

type Cell = Maybe Int
type Row  = [Cell]

data Sudoku = Sudoku [Row]
 deriving ( Show, Eq )

rows :: Sudoku -> [Row]
rows (Sudoku ms) = ms

isSudoku :: Sudoku -> Bool
isSudoku (Sudoku [[cs]]) = length [cs] == 9 && length cs == 9
isSudoku (Sudoku _)      = False

Many thanks in advance for any advice given!

[x] as a pattern will only match a singleton list (list with exactly one element in it).

To perform the nested lists check, do

isSudokuList cs  =  length cs == ...    &&
                      and [length c == ... | c <- cs]

You will have to tweak it to fit your types of course.

You could also define

niner [a,b,c,d,e,f,g,h,i] = True
.......

and use it .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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