I’ve been trying to make a function in Haskell that has as an input a list that consists of lists and (it might sound pretty simple) I want this function to check if the input is a correct table with (m,n) dimensions. If it’s not, it will return (0,0). For example, if I input:
[[1,7,2,1],[2,2,7,8],[3,2,0,1]]
I’ll get:
(3,4)
I need this function for rejecting invalid inputs in my program.
First of all, to answer your actual question, don’t use
(0, 0)as the exceptional result. Your function’s type should be:If the matrix is invalid, the result will be
Nothing. Now first check what the length of the first sublist is. I’m assuming that a matrix must have at least one row and one column:Now you can use the fact that
Maybeis a monad:And now let’s get back to the actual problem at hand: A list of lists is not what you want. A much better data type for this kind of application is an array as defined in one of the
Data.Array.*modules. A more experimental alternative, where you get parallelization for free, is to use repa.