Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell - check whether a 2D list has the same number of rows as columns

I have a 2D list [[Int]] in Haskell and I want to check two things:

  1. whether the list has the sam number of rows as columns
  2. whether the rows have the sam number of elements

For instance:

[[1,2,3], [1,55,9]] has the same number of rows as columns - here 2 - and each row has the same number of elements namely 3.

But

[[1,2], [1,55], [4,7]] has the same number of elements in each row though it has unequal number of rows and columns namely 3r 2c.

yet another example:

[[1,2], [1,55], [4,7,8]] has neither the same number of rows as columns nor each row has the same number of elements.

Actually step 1 includes step 2, am I right??

My attempt:

So what I attempted so far, is this:

listIsEqual :: [[Int]] -> Bool
listIsEqual myList = (all (\x -> length x == (length myList)) )

Right now I get the following error mesage:

Couldn't match expected type `Bool' with actual type `[a0] -> Bool'
In the return type of a call of `all'
Probable cause: `all' is applied to too few arguments
In the expression: (all (\ x -> length x == (length myList)))
In an equation for `listIsEqual':
            listIsEqual myList = (all (\ x -> length x == (length myList)))

Could anyone tell me where the problem is?

Is there also any other ways to solve this problem?

like image 871
John Avatar asked Jan 13 '23 10:01

John


1 Answers

GHC's error messages aren't always the most helpful, but in this case it got it right.

Probable cause: `all' is applied to too few arguments

And indeed, you forgot the second argument to all:

listIsEqual myList = all (\x -> length x == length myList) myList
                                                           ^^^^^^
like image 174
hammar Avatar answered Jan 25 '23 22:01

hammar