I'm trying to make a function in haskell to know if all the elements in a list of list have the same length. (I've search answers in previous posts but none of them works).
sameLength :: [[t]] -> String
sameLength [] = "Empty list"
sameLength [[items]]
| and $ map (\x -> length x == (length $ head [[items]])) [[items]] = "Same length"
| otherwise = "Not the same length"
The problem is that it doesn't work :
*Main> :l test.hs
[1 of 1] Compiling Main ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> sameLength []
"Empty list"
*Main> sameLength [[1,2],[3,4]]
"*** Exception: test.hs:(2,1)-(5,39): Non-exhaustive patterns in function sameLength
*Main> sameLength [[1,2]]
"*** Exception: test.hs:(2,1)-(5,39): Non-exhaustive patterns in function sameLength
I don't really see where is the problem. It treat the case in which the parameter is an empty list and in which it is not. Am I wrong ? Did i miss something ?
Thanks for your help :)